package com.prolog.rdc.tms.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.log.aop.annotation.UserLog;
import com.prolog.product.core.easyexcel.util.ExcelUtils;
import com.prolog.rdc.tms.constant.enums.BusinessTypeEnum;
import com.prolog.rdc.tms.model.dto.loadingbill.BmsLoadingBillDTO;
import com.prolog.rdc.tms.model.dto.shipbill.BmsShipBillDTO;
import com.prolog.rdc.tms.model.vo.loadingbill.BmsLoadingBillQueryVO;
import com.prolog.rdc.tms.model.vo.loadingbill.BmsLoadingBillVO;
import com.prolog.rdc.tms.service.BmsLoadingBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hongbo.pan
 * @date 2023/4/13
 */
@RestController
@Api(tags = "BMS-支出派车单管理")
@RequestMapping("/bmsLoadingBill/pay")
public class BmsPayLoadingBillController {

    @Autowired
    private BmsLoadingBillService service;

    @UserLog("派车单列表查询")
    @ApiOperation(value = "派车单列表查询", notes = "派车单列表查询")
    @PostMapping("/findByCondition")
    public RestMessage<Page<BmsLoadingBillDTO>> findByCondition(@RequestBody @Valid BmsLoadingBillQueryVO vo) {
        vo.setBusinessTypeHidden(BusinessTypeEnum.PAY.getCode());
        return RestMessage.newInstance(true, "操作成功", service.findByCondition(vo));
    }

    @UserLog("派车单详情")
    @ApiOperation(value = "派车单详情", notes = "派车单详情")
    @PostMapping("/detailById")
    public RestMessage<BmsLoadingBillDTO> detailById(@RequestParam("id") Long id) {
        return RestMessage.newInstance(true, "操作成功", service.detailById(id));
    }

    @UserLog("派车单运单信息")
    @ApiOperation(value = "派车单运单信息", notes = "派车单运单信息")
    @PostMapping("/getShipBill")
    public RestMessage<List<BmsShipBillDTO>> getShipBill(@RequestParam("id") Long id) {
        return RestMessage.newInstance(true, "操作成功", service.getShipBill(id));
    }

    @UserLog("派车单更新")
    @ApiOperation(value = "派车单更新", notes = "派车单更新")
    @PostMapping("/update")
    public RestMessage<Object> update(@RequestBody @Valid BmsLoadingBillVO vo) {
        vo.setBusinessTypeHidden(BusinessTypeEnum.PAY.getCode());
        service.saveOrUpdate(vo);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("派车单核算")
    @ApiOperation(value = "派车单核算", notes = "派车单核算")
    @PostMapping("/calculate")
    public RestMessage<Object> calculate(@RequestBody List<Long> idList) {
        service.calculate(idList, BusinessTypeEnum.PAY);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("派车单提交")
    @ApiOperation(value = "派车单提交", notes = "派车单提交")
    @PostMapping("/submit")
    public RestMessage<Object> submit(@RequestBody List<Long> idList) {
        service.submit(idList, BusinessTypeEnum.PAY);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("派车单导出")
    @ApiOperation(value = "派车单导出", notes = "派车单导出")
    @PostMapping("/export")
    public RestMessage<Object> export(@RequestBody BmsLoadingBillQueryVO vo, HttpServletResponse response) throws Exception {
        vo.setBusinessTypeHidden(BusinessTypeEnum.PAY.getCode());
        vo.setPageNum(1);
        vo.setPageSize(Integer.MAX_VALUE);
        Page<BmsLoadingBillDTO> page = service.findByCondition(vo);
        ExcelUtils.export(response, BmsLoadingBillDTO.class, DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_FORMAT), exportFormat(page));
        return null;
    }

    @UserLog("派车单分页导出")
    @ApiOperation(value = "派车单分页导出", notes = "派车单分页导出")
    @PostMapping("/exportPage")
    public RestMessage<Object> exportPage(@RequestBody BmsLoadingBillQueryVO vo, HttpServletResponse response) throws Exception {
        vo.setBusinessTypeHidden(BusinessTypeEnum.PAY.getCode());
        Page<BmsLoadingBillDTO> page = service.findByCondition(vo);
        ExcelUtils.export(response, BmsLoadingBillDTO.class, DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_FORMAT), exportFormat(page));
        return null;
    }

    @UserLog("派车单选中行导出")
    @ApiOperation(value = "派车单选中行导出", notes = "派车单选中行导出")
    @PostMapping("/exportSelected")
    public RestMessage<Object> exportSelected(@RequestBody List<Long> idList, HttpServletResponse response) throws Exception {
        BmsLoadingBillQueryVO vo = new BmsLoadingBillQueryVO();
        vo.setBusinessTypeHidden(BusinessTypeEnum.PAY.getCode());
        vo.setPageNum(1);
        vo.setPageSize(Integer.MAX_VALUE);
        vo.setIdList(idList);
        Page<BmsLoadingBillDTO> page = service.findByCondition(vo);
        ExcelUtils.export(response, BmsLoadingBillDTO.class, DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_FORMAT), exportFormat(page));
        return null;
    }

    private List<BmsLoadingBillDTO> exportFormat(Page<BmsLoadingBillDTO> page) {
        List<BmsLoadingBillDTO> list = page.getList();
        if (CollectionUtil.isNotEmpty(list)) {
            list = list.stream().peek(bmsLoadingBillDTO -> {
                bmsLoadingBillDTO.setTotalPcs(bmsLoadingBillDTO.getTotalPayPcs());
                bmsLoadingBillDTO.setTotalWeight(bmsLoadingBillDTO.getTotalPayWeight());
                bmsLoadingBillDTO.setTotalVolume(bmsLoadingBillDTO.getTotalPayVolume());
                bmsLoadingBillDTO.setTotalMileage(bmsLoadingBillDTO.getTotalPayMileage());
                bmsLoadingBillDTO.setTotalMileageMap(bmsLoadingBillDTO.getTotalPayMileageMap());
                bmsLoadingBillDTO.setFeeCltTypeName(bmsLoadingBillDTO.getPayFeeCltTypeName());
            }).collect(Collectors.toList());
        }
        return list;
    }
}