package com.dcsoft.system.car.controller;

import com.dcsoft.common.core.exception.ServiceException;
import com.dcsoft.common.core.utils.CollUtil;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.web.page.TableDataInfo;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.system.car.domain.TbCar;
import com.dcsoft.system.car.service.ITbCarService;
import com.dcsoft.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 车辆信息Controller
 *
 * @author nichun
 * @date 2023-07-24
 */
@RestController
@RequestMapping("/car")
public class TbCarController extends BaseController
{
    @Autowired
    private ITbCarService tbCarService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询车辆信息列表
     */
//    @RequiresPermissions("system:car:list")
    @GetMapping("/list")
    public TableDataInfo list(TbCar tbCar)
    {
        startPage();
        List<TbCar> list = tbCarService.selectTbCarList(tbCar);
        return getDataTable(list);
    }

    /**
     * 小程序查询车辆信息列表
     */
//    @RequiresPermissions("system:car:list")
    @GetMapping("/app/list")
    public TableDataInfo appList(TbCar tbCar)
    {
        startPage();
        List<TbCar> list = tbCarService.selectAppTbCarList(tbCar);
        Map<String, String> map = dictDataService.queryDictData("sys_car_colour");
        try {
            for (TbCar car : list) {
                if (StringUtils.isNotBlank(car.getPlateColor())) {
                    car.setPlateColorName(map.get(car.getPlateColor()));
                }
            }
        } catch (ServiceException e) {
            throw new ServiceException(e.getMessage());
        }
        return getDataTable(list);
    }

    /**
     * 导出车辆信息列表
     */
    @RequiresPermissions("system:car:export")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbCar tbCar)
    {
        List<TbCar> list = tbCarService.selectTbCarList(tbCar);
        ExcelUtil<TbCar> util = new ExcelUtil<TbCar>(TbCar.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
//    @RequiresPermissions("system:car:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        TbCar tbCar = tbCarService.selectTbCarById(id);
        Map<String, String> map = dictDataService.queryDictData("sys_car_colour");
        if (StringUtils.isNotBlank(tbCar.getPlateColor())) {
            tbCar.setPlateColorName(map.get(tbCar.getPlateColor()));
        }
        return success(tbCar);
    }

    /**
     * 新增车辆信息
     */
//    @RequiresPermissions("system:car:add")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCar tbCar)
    {
        TbCar query = new TbCar();
        query.setCarNo(tbCar.getCarNo());
        List<TbCar> tbCars = tbCarService.selectTbCarList(query);
        if (CollUtil.isNotEmpty(tbCars)) {
            throw new ServiceException("该车牌已被绑定，请重新添加车辆信息");
        }
        if (null != tbCar && null != tbCar.getId()) {
            return toAjax(tbCarService.updateTbCar(tbCar));
        }
        return toAjax(tbCarService.insertTbCar(tbCar));
    }

    /**
     * 修改车辆信息
     */
//    @RequiresPermissions("system:car:edit")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCar tbCar)
    {
        return toAjax(tbCarService.updateTbCar(tbCar));
    }

    /**
     * 删除车辆信息
     */
//    @RequiresPermissions("system:car:remove")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbCarService.deleteTbCarByIds(ids));
    }
}
