package com.bs.cm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.bs.cm.domain.CmVehicle;
import com.bs.cm.domain.CmVehicleRfid;
import com.bs.cm.domain.CmVehicleRfidDate;
import com.bs.cm.service.ICmVehicleRfidDateService;
import com.bs.cm.service.ICmVehicleService;
import com.bs.common.constant.HttpStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
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.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import com.bs.cm.domain.CmVehicleHost;
import com.bs.cm.service.ICmVehicleHostService;
import javax.annotation.Resource;

/**
 * 车辆主机信息Controller
 *
 * @author bs
 * @date 2025-01-12
 */
@Api(tags = "车辆主机信息")
@RestController
@RequestMapping("/cm/vehicle/host")
public class CmVehicleHostController extends BaseController {
    @Resource
    private ICmVehicleHostService cmVehicleHostService;
    @Resource
    private ICmVehicleService cmVehicleService;

    /**
     * 分页查询车辆主机信息列表
     */
    @ApiOperation("分页查询车辆主机信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CmVehicleHost cmVehicleHost) {
        startPage();
        LambdaQueryWrapper<CmVehicleHost> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmVehicleHost);
        List<CmVehicleHost> list = cmVehicleHostService.list(queryWrapper);
        list = cmVehicleHostService.getVehicleInfo(list);
        return getDataTable(list);
    }

    /**
     * 查询车辆主机信息列表
     */
    @ApiOperation("查询车辆主机信息列表")
    @GetMapping("/list")
    public AjaxResult list(CmVehicleHost cmVehicleHost) {
        LambdaQueryWrapper<CmVehicleHost> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmVehicleHost);
        List<CmVehicleHost> list = cmVehicleHostService.list(queryWrapper);
        list = cmVehicleHostService.getVehicleInfo(list);
        return success(list);
    }

    /**
     * 检查车辆RFID状态
     */
    @ApiOperation("检查车辆主机状态")
    @GetMapping("/check")
    public AjaxResult check(CmVehicleHost cmVehicleHost) {
        if(Validator.isNotEmpty(cmVehicleHost.getHostType()) && Validator.isNotEmpty(cmVehicleHost.getHostNo()) && Validator.isNotEmpty(cmVehicleHost.getVehicleId())){
            LambdaQueryWrapper<CmVehicleHost> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(CmVehicleHost::getVehicleId, cmVehicleHost.getVehicleId());
            queryWrapper.eq(CmVehicleHost::getHostNo, cmVehicleHost.getHostNo());
            queryWrapper.eq(CmVehicleHost::getHostType, cmVehicleHost.getHostType());
            queryWrapper.eq(CmVehicleHost::getIsValid, "Y");
            List<CmVehicleHost> list = cmVehicleHostService.list(queryWrapper);
            if (null != list && list.size() > 0) {
                return error("此主机已经在该车工作，请勿重复绑定");
            } else {
                queryWrapper = new LambdaQueryWrapper();
                queryWrapper.ne(CmVehicleHost::getVehicleId, cmVehicleHost.getVehicleId());
                queryWrapper.eq(CmVehicleHost::getHostNo, cmVehicleHost.getHostNo());
                queryWrapper.eq(CmVehicleHost::getHostType, cmVehicleHost.getHostType());
                queryWrapper.eq(CmVehicleHost::getIsValid, "Y");
                list = cmVehicleHostService.list(queryWrapper);
                if (null != list && list.size() > 0) {
                    CmVehicle vehicle = cmVehicleService.getById(list.get(0).getVehicleId());
                    if (null != vehicle) {
                        return new AjaxResult(HttpStatus.SUCCESS, "此主机已经在其他车辆" + vehicle.getVehicleNo() +"绑定，是否换绑到本车？", vehicle, "ok");
                    } else {
                        return success(new CmVehicle());
                    }
                } else {
                    return success(new CmVehicle());
                }
            }
        } else {
            return error("请输入主机类型、编号和车辆信息");
        }
    }

    /**
     * 导出车辆主机信息列表
     */
    @ApiOperation("导出车辆主机信息列表")
    @Log(title = "车辆主机信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmVehicleHost cmVehicleHost) {
        LambdaQueryWrapper<CmVehicleHost> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmVehicleHost);
        List<CmVehicleHost> list = cmVehicleHostService.list(queryWrapper);
        ExcelUtil<CmVehicleHost> util = new ExcelUtil<CmVehicleHost>(CmVehicleHost. class);
        util.exportExcel(response, list, "车辆主机信息数据");
    }

    /**
     * 获取车辆主机信息详细信息
     */
    @ApiOperation("获取车辆主机信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        CmVehicleHost host = cmVehicleHostService.getById(id);
        if (null != host) {
            CmVehicle vehicle = cmVehicleService.getById(host.getVehicleId());
            host.setVehicle(vehicle);
        }
        return success(host);
    }

    /**
     * 新增车辆主机信息
     */
    @ApiOperation("新增车辆主机信息")
    @Log(title = "车辆主机信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmVehicleHost cmVehicleHost) {
        if(Validator.isNotEmpty(cmVehicleHost.getHostType()) && Validator.isNotEmpty(cmVehicleHost.getHostNo()) && Validator.isNotEmpty(cmVehicleHost.getVehicleId())){
            LambdaQueryWrapper<CmVehicleHost> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(CmVehicleHost::getVehicleId, cmVehicleHost.getVehicleId());
            queryWrapper.eq(CmVehicleHost::getHostNo, cmVehicleHost.getHostNo());
            queryWrapper.eq(CmVehicleHost::getHostType, cmVehicleHost.getHostType());
            queryWrapper.eq(CmVehicleHost::getIsValid, "Y");
            List<CmVehicleHost> list = cmVehicleHostService.list(queryWrapper);
            if (null != list && list.size() > 0) {
                return error("此主机已经在该车工作，请勿重复绑定");
            } else {
                queryWrapper = new LambdaQueryWrapper();
                queryWrapper.ne(CmVehicleHost::getVehicleId, cmVehicleHost.getVehicleId());
                queryWrapper.eq(CmVehicleHost::getHostNo, cmVehicleHost.getHostNo());
                queryWrapper.eq(CmVehicleHost::getHostType, cmVehicleHost.getHostType());
                queryWrapper.eq(CmVehicleHost::getIsValid, "Y");
                list = cmVehicleHostService.list(queryWrapper);
                // 如果在其他车辆绑定
                if (null != list && list.size() > 0) {
                    for (CmVehicleHost host : list) {
                        host.setIsValid("N");
                        host.setExpiringDate(new Date());
                    }
                    cmVehicleHostService.updateBatchById(list);
                }
                cmVehicleHostService.save(cmVehicleHost);
                return success("绑定成功");
            }
        } else {
            return error("请输入主机编号和车辆信息");
        }
    }

    /**
     * 修改车辆主机信息
     */
    @ApiOperation("修改车辆主机信息")
    @Log(title = "车辆主机信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmVehicleHost cmVehicleHost) {
        return toAjax(cmVehicleHostService.updateById(cmVehicleHost));
    }

    /**
     * 删除车辆主机信息
     */
    @ApiOperation("删除车辆主机信息")
    @Log(title = "车辆主机信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        if (null != ids && ids.size() > 0) {
            List<CmVehicleHost> list = cmVehicleHostService.listByIds(ids);
            if (null != list && list.size() > 0) {
                for (CmVehicleHost host : list) {
                    host.setIsValid("N");
                    host.setExpiringDate(new Date());
                }
                cmVehicleHostService.updateBatchById(list);
            }
            return success("解绑成功");
        } else {
            return error("请选择需要解绑的车辆主机");
        }
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CmVehicleHost> queryWrapper,CmVehicleHost cmVehicleHost){


        //车辆主机ID
        if(Validator.isNotEmpty(cmVehicleHost.getId())){
            queryWrapper.eq(CmVehicleHost::getId,cmVehicleHost.getId());
        }

        //车辆ID
        if(Validator.isNotEmpty(cmVehicleHost.getVehicleId())){
            queryWrapper.eq(CmVehicleHost::getVehicleId,cmVehicleHost.getVehicleId());
        }

        //设备类型
        if(Validator.isNotEmpty(cmVehicleHost.getHostType())){
            queryWrapper.eq(CmVehicleHost::getHostType,cmVehicleHost.getHostType());
        }

        //厂商
        if(Validator.isNotEmpty(cmVehicleHost.getManufacturer())){
            queryWrapper.eq(CmVehicleHost::getManufacturer,cmVehicleHost.getManufacturer());
        }

        //主机编号
        if(Validator.isNotEmpty(cmVehicleHost.getHostNo())){
            queryWrapper.eq(CmVehicleHost::getHostNo,cmVehicleHost.getHostNo());
        }

        //流量卡号
        if(Validator.isNotEmpty(cmVehicleHost.getSimNo())){
            queryWrapper.eq(CmVehicleHost::getSimNo,cmVehicleHost.getSimNo());
        }

        //安装日期
        if(Validator.isNotEmpty(cmVehicleHost.getSetupDate())){
            queryWrapper.eq(CmVehicleHost::getSetupDate,cmVehicleHost.getSetupDate());
        }

        //生效日期
        if(Validator.isNotEmpty(cmVehicleHost.getEffectiveDate())){
            queryWrapper.eq(CmVehicleHost::getEffectiveDate,cmVehicleHost.getEffectiveDate());
        }

        //失效日期
        if(Validator.isNotEmpty(cmVehicleHost.getExpiringDate())){
            queryWrapper.eq(CmVehicleHost::getExpiringDate,cmVehicleHost.getExpiringDate());
        }

        //最后工作时间
        if(Validator.isNotEmpty(cmVehicleHost.getLastWorkDate())){
            queryWrapper.eq(CmVehicleHost::getLastWorkDate,cmVehicleHost.getLastWorkDate());
        }

        //是否有效
        if(Validator.isNotEmpty(cmVehicleHost.getIsValid())){
            queryWrapper.eq(CmVehicleHost::getIsValid,cmVehicleHost.getIsValid());
        }

        //备注
        if(Validator.isNotEmpty(cmVehicleHost.getRemark())){
            queryWrapper.eq(CmVehicleHost::getRemark,cmVehicleHost.getRemark());
        }

        //删除标志（0代表存在 2代表删除）
        if(Validator.isNotEmpty(cmVehicleHost.getDelFlag())){
            queryWrapper.eq(CmVehicleHost::getDelFlag,cmVehicleHost.getDelFlag());
        }

        //创建部门
        if(Validator.isNotEmpty(cmVehicleHost.getCreateDept())){
            queryWrapper.eq(CmVehicleHost::getCreateDept,cmVehicleHost.getCreateDept());
        }

        //创建人
        if(Validator.isNotEmpty(cmVehicleHost.getCreateBy())){
            queryWrapper.eq(CmVehicleHost::getCreateBy,cmVehicleHost.getCreateBy());
        }

        //创建时间
        if(Validator.isNotEmpty(cmVehicleHost.getCreateTime())){
            queryWrapper.eq(CmVehicleHost::getCreateTime,cmVehicleHost.getCreateTime());
        }

        //修改人
        if(Validator.isNotEmpty(cmVehicleHost.getUpdateBy())){
            queryWrapper.eq(CmVehicleHost::getUpdateBy,cmVehicleHost.getUpdateBy());
        }

        //修改时间
        if(Validator.isNotEmpty(cmVehicleHost.getUpdateTime())){
            queryWrapper.eq(CmVehicleHost::getUpdateTime,cmVehicleHost.getUpdateTime());
        }

        //租户ID
        if(Validator.isNotEmpty(cmVehicleHost.getTenantId())){
            queryWrapper.eq(CmVehicleHost::getTenantId,cmVehicleHost.getTenantId());
        }

        }

}
