package com.bs.jxc.controller;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.bs.jxc.domain.CkVehicleList;
import com.bs.jxc.service.ICarBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆批量出库Controller
 *
 * @author bs
 * @date 2023-11-01
 */
@Api(tags = "车辆批量出库")
@RestController
@RequestMapping("/jxc/billCar")
public class CarBillController extends BaseController {
    @Resource
    private ICarBillService carBillService;


    @Log(title = "导入车辆批量出库资料", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String billCargoNumber,String maxWtStr,String outBillNumber) {
        try {
            BigDecimal maxWt = new BigDecimal(maxWtStr); // 将字符串转换为 BigDecimal 类型
            ExcelUtil<CkVehicleList> util = new ExcelUtil<>(CkVehicleList.class);
            List<CkVehicleList> carList = util.importExcel(file.getInputStream());

            // 创建 LambdaQueryWrapper 对象
            LambdaQueryWrapper<CkVehicleList> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CkVehicleList::getBillCargoNumber,billCargoNumber)
                    .eq(CkVehicleList::getOutBillNumber,outBillNumber);

            List<CkVehicleList> list1 = carBillService.list(queryWrapper);

            BigDecimal sumJobWt1 = list1.stream()
                    .map(CkVehicleList::getJobWt)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            // 根据具体需求进行导入操作
            for (CkVehicleList ckVehicleList : carList) {
                // 按照业务需求构建查询条件
                BigDecimal jobWt = ckVehicleList.getJobWt();
                sumJobWt1 = sumJobWt1.add(jobWt); // 累加 jobWt
                if (sumJobWt1.compareTo(maxWt) > 0) { // 如果超过库存数量
                    return AjaxResult.error("导入失败，出库量大于库存" );
                }

            }
            for (CkVehicleList ckVehicleList : carList) {
                // 按照业务需求构建查询条件
                ckVehicleList.setOutBillNumber(outBillNumber);

                ckVehicleList.setBillCargoNumber(billCargoNumber);
                carBillService.save(ckVehicleList);

            }
            LambdaQueryWrapper<CkVehicleList> queryWrapperVo = new LambdaQueryWrapper<>();
            queryWrapperVo.eq(CkVehicleList::getBillCargoNumber,billCargoNumber)
                    .eq(CkVehicleList::getOutBillNumber,outBillNumber);

            List<CkVehicleList> list = carBillService.list(queryWrapperVo);

            BigDecimal sumJobWt = list.stream()
                    .map(CkVehicleList::getJobWt)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);


            return AjaxResult.success("导入成功，总的重量为：" + sumJobWt);
        } catch (Exception e) {
            return AjaxResult.error("导入失败：" + e.getMessage());
        }
    }

    @ApiOperation("导入车辆批量出库资料列表模板")
    @Log(title = "导入车辆批量出库资料模板", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response, CkVehicleList ckVehicleList) {
        // 创建一个空的船舶基本资料列表
        List<CkVehicleList> emptyList = new ArrayList<>();

        // 导出空列表为 Excel 文件
        ExcelUtil<CkVehicleList> util = new ExcelUtil<>(CkVehicleList.class);
        util.exportExcel(response, emptyList, "船舶基本资料模板");
    }



    /**
     * 分页查询车辆批量出库列表
     */
    @ApiOperation("分页查询车辆批量出库列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CkVehicleList ckVehicleList) {
        startPage();
        LambdaQueryWrapper<CkVehicleList> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper, ckVehicleList);
        List<CkVehicleList> list = carBillService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询车辆批量出库列表
     */
    @ApiOperation("查询车辆批量出库列表")
    @GetMapping("/list")
    public AjaxResult list(CkVehicleList ckVehicleList) {
        LambdaQueryWrapper<CkVehicleList> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper, ckVehicleList);
        List<CkVehicleList> list = carBillService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出车辆批量出库列表
     */
    @ApiOperation("导出车辆批量出库列表")
    @Log(title = "车辆批量出库导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CkVehicleList ckVehicleList) {
        LambdaQueryWrapper<CkVehicleList> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper, ckVehicleList);
        List<CkVehicleList> list = carBillService.list(queryWrapper);
        ExcelUtil<CkVehicleList> util = new ExcelUtil<CkVehicleList>(CkVehicleList. class);
        util.exportExcel(response, list, "车辆批量出库数据");
    }

    /**
     * 获取车辆批量出库详细信息
     */
    @ApiOperation("获取车辆批量出库详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(carBillService.getById(id));
    }

    /**
     * 新增车辆批量出库
     */
    @ApiOperation("新增车辆批量出库")
    @Log(title = "车辆批量出库新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CkVehicleList ckVehicleList) {
        return toAjax(carBillService.save(ckVehicleList));
    }

    /**
     * 修改车辆批量出库
     */
    @ApiOperation("修改车辆批量出库")
    @Log(title = "车辆批量出库修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CkVehicleList ckVehicleList) {
        return toAjax(carBillService.updateById(ckVehicleList));
    }

    /**
     * 删除车辆批量出库
     */
    @ApiOperation("删除车辆批量出库")
    @Log(title = "车辆批量出库删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(carBillService.removeBatchByIds(ids));
    }

    /**
     * 关联删除车辆批量出库
     */
//    @ApiOperation("关联删除车辆批量出库")
//    @Log(title = "关联车辆批量出库删除", businessType = BusinessType.DELETE)
//    @DeleteMapping("/removeByNum")
//    public AjaxResult removeByNum( String billCargoNumber,String outBillNumber) {
//        LambdaQueryWrapper<CkVehicleList> queryWrapperVo = new LambdaQueryWrapper<>();
//        queryWrapperVo.eq(CkVehicleList::getBillCargoNumber,billCargoNumber)
//                .eq(CkVehicleList::getOutBillNumber,outBillNumber);
//
//        List<CkVehicleList> list = carBillService.list(queryWrapperVo);
//        List<Long> ids = new ArrayList<>();
//        for (CkVehicleList vehicle : list) {
//            ids.add(vehicle.getId());
//        }
//
//        return toAjax(carBillService.removeBatchByIds(ids));
//    }


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


        //主键
        if(ckVehicleList.getId()!=null){
            queryWrapper.eq(CkVehicleList::getId, ckVehicleList.getId());
        }

        //单据编号
        if(ckVehicleList.getBillCargoNumber() != null){
            queryWrapper.eq(CkVehicleList::getBillCargoNumber, ckVehicleList.getBillCargoNumber());
        }

        //车牌号
        if(Validator.isNotEmpty(ckVehicleList.getCarCard())){
            queryWrapper.eq(CkVehicleList::getCarCard, ckVehicleList.getCarCard());
        }

        //出库单据编号
        if(Validator.isNotEmpty(ckVehicleList.getOutBillNumber())){
            queryWrapper.eq(CkVehicleList::getOutBillNumber, ckVehicleList.getOutBillNumber());
        }

        //业务类型
        if(Validator.isNotEmpty(ckVehicleList.getBusinessType())){
            queryWrapper.eq(CkVehicleList::getBusinessType, ckVehicleList.getBusinessType());
        }

        //车辆类型
        if(Validator.isNotEmpty(ckVehicleList.getCarType())){
            queryWrapper.eq(CkVehicleList::getCarType, ckVehicleList.getCarType());
        }

//        //船舶ID
//        if(Validator.isNotEmpty(carBill.getVesselId())){
//            queryWrapper.eq(CarBill::getVesselId,carBill.getVesselId());
//        }

        //船舶名称
        if(Validator.isNotEmpty(ckVehicleList.getVesselName())){
            queryWrapper.eq(CkVehicleList::getVesselName, ckVehicleList.getVesselName());
        }

        //泊位代码
        if(Validator.isNotEmpty(ckVehicleList.getBerthCode())){
            queryWrapper.eq(CkVehicleList::getBerthCode, ckVehicleList.getBerthCode());
        }

        //泊位名称
        if(Validator.isNotEmpty(ckVehicleList.getBerthName())){
            queryWrapper.eq(CkVehicleList::getBerthName, ckVehicleList.getBerthName());
        }

//        //入闸时间
//        if(Validator.isNotEmpty(carBill.getEntryTime())){
//            queryWrapper.eq(CarBill::getEntryTime,carBill.getEntryTime());
//        }
//
//        //出闸时间
//        if(Validator.isNotEmpty(carBill.getExitTime())){
//            queryWrapper.eq(CarBill::getExitTime,carBill.getExitTime());
//        }

        //作业确认 1:未确认，2：已确认
        if(Validator.isNotEmpty(ckVehicleList.getJobConfirm())){
            queryWrapper.eq(CkVehicleList::getJobConfirm, ckVehicleList.getJobConfirm());
        }

//        //货物ID
//        if(Validator.isNotEmpty(carBill.getCargoId())){
//            queryWrapper.eq(CarBill::getCargoId,carBill.getCargoId());
//        }

        //商品名称
        if(Validator.isNotEmpty(ckVehicleList.getCargoName())){
            queryWrapper.eq(CkVehicleList::getCargoName, ckVehicleList.getCargoName());
        }

        //唛头
        if(Validator.isNotEmpty(ckVehicleList.getMark())){
            queryWrapper.eq(CkVehicleList::getMark, ckVehicleList.getMark());
        }

        //货主名称
        if(Validator.isNotEmpty(ckVehicleList.getCustName())){
            queryWrapper.eq(CkVehicleList::getCustName, ckVehicleList.getCustName());
        }

//        //入闸车重
//        if(Validator.isNotEmpty(carBill.getEntryWt())){
//            queryWrapper.eq(CarBill::getEntryWt,carBill.getEntryWt());
//        }
//
//        //出闸车重
//        if(Validator.isNotEmpty(carBill.getExitWt())){
//            queryWrapper.eq(CarBill::getExitWt,carBill.getExitWt());
//        }
//
//        //作业数量
//        if(Validator.isNotEmpty(carBill.getJobNum())){
//            queryWrapper.eq(CarBill::getJobNum,carBill.getJobNum());
//        }
//
//        //作业重量
//        if(Validator.isNotEmpty(carBill.getJobWt())){
//            queryWrapper.eq(CarBill::getJobWt,carBill.getJobWt());
//        }

        //委托单流水号
        if(Validator.isNotEmpty(ckVehicleList.getOrderNum())){
            queryWrapper.eq(CkVehicleList::getOrderNum, ckVehicleList.getOrderNum());
        }

        //所属作业区
        if(Validator.isNotEmpty(ckVehicleList.getJobArea())){
            queryWrapper.eq(CkVehicleList::getJobArea, ckVehicleList.getJobArea());
        }

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

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

//        //创建部门
//        if(Validator.isNotEmpty(carBill.getCreateDept())){
//            queryWrapper.eq(CarBill::getCreateDept,carBill.getCreateDept());
//        }
//
//        //创建人
//        if(Validator.isNotEmpty(carBill.getCreateBy())){
//            queryWrapper.eq(CarBill::getCreateBy,carBill.getCreateBy());
//        }
//
//        //创建时间
//        if(Validator.isNotEmpty(carBill.getCreateTime())){
//            queryWrapper.eq(CarBill::getCreateTime,carBill.getCreateTime());
//        }
//
//        //修改人
//        if(Validator.isNotEmpty(carBill.getUpdateBy())){
//            queryWrapper.eq(CarBill::getUpdateBy,carBill.getUpdateBy());
//        }
//
//        //修改时间
//        if(Validator.isNotEmpty(carBill.getUpdateTime())){
//            queryWrapper.eq(CarBill::getUpdateTime,carBill.getUpdateTime());
//        }

        }

}
