package com.dcsoft.system.vehicle.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dcsoft.common.security.utils.SecurityUtils;
import com.dcsoft.system.domain.SysEquipment;
import com.dcsoft.system.kq.domain.KqAdjust;
import com.dcsoft.system.service.ISysEquipmentService;
import com.dcsoft.system.uniubi.service.ISysVehicleService;
import com.dcsoft.system.vehicle.domain.CarInfo;
import com.dcsoft.system.vehicle.domain.CarParkItem;
import com.dcsoft.system.vehicle.domain.CarParkRecord;
import com.dcsoft.system.vehicle.service.ICarInfoService;
import com.dcsoft.system.vehicle.service.ICarParkItemService;
import com.dcsoft.system.vehicle.service.ICarParkRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车辆信息Controller
 *
 * @author nichun
 * @date 2023-12-26
 */
@RestController
@RequestMapping("/carInfo")
public class CarInfoController extends BaseController
{
    @Autowired
    private ICarInfoService carInfoService;

    @Autowired
    private ICarParkRecordService carParkRecordService;

    @Autowired
    private ICarParkItemService carParkItemService;

    /**
     * 查询车辆信息列表
     */
    @RequiresPermissions("system:carInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(CarInfo carInfo)
    {
        startPage();
        List<CarInfo> list = carInfoService.selectCarInfoList(carInfo);
        return getDataTable(list);
    }

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

    @Log(title = "车辆信息", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:carInfo:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CarInfo> util = new ExcelUtil<CarInfo>(CarInfo.class);
        List<CarInfo> userList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = carInfoService.importData(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<CarInfo> util = new ExcelUtil<CarInfo>(CarInfo.class);
        util.importTemplateExcel(response, "出勤调整数据");
    }


    /**
     * 获取车辆信息详细信息
     */
    @RequiresPermissions("system:carInfo:query")
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") Long customerId)
    {
        return success(carInfoService.selectCarInfoByCustomerId(customerId));
    }

    /**
     * 新增车辆信息
     */
    @RequiresPermissions("system:carInfo:add")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarInfo carInfo)
    {
        int i=carInfoService.insertCarInfo(carInfo);
        //保存车辆授权
        //根据车场查询车辆
        if(carInfo.getParkId()!=null){
            CarParkItem carParkItem=new CarParkItem();
            carParkItem.setParkId(carInfo.getParkId());
            List<CarParkItem> itemList=carParkItemService.selectCarParkItemList(carParkItem);
            for(CarParkItem a:itemList){
                CarParkRecord carParkRecord=new CarParkRecord();
                carParkRecord.setCustomerId(carInfo.getCustomerId());
                carParkRecord.setParkId(carInfo.getParkId());
                carParkRecord.setEquipmentId(a.getEquipmentId());
                carParkRecordService.insertCarParkRecord(carParkRecord);
            }
        }
        return toAjax(i);
    }

    /**
     * 修改车辆信息
     */
    @RequiresPermissions("system:carInfo:edit")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarInfo carInfo)
    {
        //根据车场查询车辆
        if(carInfo.getParkId()!=null){
            //删除车辆授权
            carParkRecordService.deleteCarParkRecordByCustomerId(carInfo.getCustomerId());
            CarParkItem carParkItem=new CarParkItem();
            carParkItem.setParkId(carInfo.getParkId());
            List<CarParkItem> itemList=carParkItemService.selectCarParkItemList(carParkItem);
            for(CarParkItem a:itemList){
                CarParkRecord carParkRecord=new CarParkRecord();
                carParkRecord.setCustomerId(carInfo.getCustomerId());
                carParkRecord.setParkId(carInfo.getParkId());
                carParkRecord.setEquipmentId(a.getEquipmentId());
                carParkRecordService.insertCarParkRecord(carParkRecord);

            }
        }
        return toAjax(carInfoService.updateCarInfo(carInfo));
    }

    /**
     * 删除车辆信息
     */
    @RequiresPermissions("system:carInfo:remove")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customerIds}")
    public AjaxResult remove(@PathVariable Long[] customerIds)
    {
        //carParkRecordService.deleteCarParkRecordByCustomerIds(customerIds);
        return toAjax(carInfoService.deleteCarInfoByCustomerIds(customerIds));
    }

    /**
     * 获取企业模块|菜单权限树
     */
    @GetMapping(value = "/tree")
    public AjaxResult tree() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("depts", carInfoService.selectTree());
        return ajax;
    }

    /**
     * 获取企业模块|菜单权限树
     */
    @GetMapping(value = "/carPlay/{id}")
    public AjaxResult carPlay(@PathVariable("id") Long id) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("plays", carInfoService.carPlay(id));
        return ajax;
    }

    /**
     * 获取企业模块|菜单权限树
     */
    @GetMapping(value = "/carPlayRecord/{id}")
    public AjaxResult carPlayRecord(@PathVariable("id") Long id) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("records", carInfoService.carPlayRecord(id));
        return ajax;
    }

    /**
     * 获取企业模块|菜单权限树
     */
    @GetMapping(value = "/selectCarInfo/{id}/{flag}")
    public AjaxResult selectCarInfo(@PathVariable("id") Long id,@PathVariable("flag") String flag) {
        return toAjax(carInfoService.selectCarInfo(id,flag));
    }

}
