package com.prolog.rdc.tms.controller;

import cn.hutool.core.collection.CollectionUtil;
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.rdc.tms.constant.enums.BusinessTypeEnum;
import com.prolog.rdc.tms.model.dto.shipbill.BmsLogOperationDTO;
import com.prolog.rdc.tms.model.dto.shipbill.BmsShipBillAdjustDTO;
import com.prolog.rdc.tms.model.dto.shipbill.BmsShipBillDTO;
import com.prolog.rdc.tms.model.dto.shipbill.BmsShipBillDetailDTO;
import com.prolog.rdc.tms.model.dto.shipbill.BmsShipBillPackageDTO;
import com.prolog.rdc.tms.model.vo.shipbill.BmsShipBillQueryVO;
import com.prolog.rdc.tms.model.vo.shipbill.BmsShipBillVO;
import com.prolog.rdc.tms.service.BmsShipBillService;
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.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

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

    @Autowired
    private BmsShipBillService service;

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

    @UserLog("运单详情")
    @ApiOperation(value = "运单详情", notes = "运单详情")
    @PostMapping("/detailById")
    public RestMessage<BmsShipBillDTO> detailById(@RequestParam("id") Long id) {
        BmsShipBillDTO dto = service.detailById(id);
        dto.setDetailList(transfer(dto.getDetailList()));
        return RestMessage.newInstance(true, "操作成功", dto);
    }

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

    @UserLog("运单更新核算")
    @ApiOperation(value = "运单更新核算", notes = "运单更新核算")
    @PostMapping("/updateAndCal")
    public RestMessage<Object> updateAndCal(@RequestBody @Valid BmsShipBillVO vo) {
        vo.setBusinessTypeHidden(BusinessTypeEnum.PAY.getCode());
        service.saveAndCal(vo,"2");
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运单商品")
    @ApiOperation(value = "运单商品", notes = "运单商品")
    @PostMapping("/getItem")
    public RestMessage<List<BmsShipBillDetailDTO>> getItem(@RequestParam("id") Long id) {
        return RestMessage.newInstance(true, "操作成功", service.getItem(id));
    }

    @UserLog("运单包裹")
    @ApiOperation(value = "运单包裹", notes = "运单包裹")
    @PostMapping("/getPackage")
    public RestMessage<List<BmsShipBillPackageDTO>> getPackage(@RequestParam("id") Long id) {
        return RestMessage.newInstance(true, "操作成功", service.getPackage(id));
    }

    @UserLog("运单调整")
    @ApiOperation(value = "运单调整", notes = "运单调整")
    @PostMapping("/getAdjust")
    public RestMessage<List<BmsShipBillAdjustDTO>> getAdjust(@RequestParam("id") Long id) {
        return RestMessage.newInstance(true, "操作成功", service.getAdjust(id));
    }

    @UserLog("运单日志")
    @ApiOperation(value = "运单日志", notes = "运单日志")
    @PostMapping("/getLog")
    public RestMessage<List<BmsLogOperationDTO>> getLog(@RequestParam("id") Long id) {
        return RestMessage.newInstance(true, "操作成功", service.getLog(id));
    }

    @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("/calculateAndSubmit")
    public RestMessage<Object> calculateAndSubmit(@RequestBody List<Long> idList) {
        service.calculateAndSubmit(idList, BusinessTypeEnum.PAY);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    /**
     * transfer
     * @param detailList
     * @return
     */
    private List<BmsShipBillDetailDTO> transfer(List<BmsShipBillDetailDTO> detailList) {
        if (CollectionUtil.isNotEmpty(detailList)) {
            detailList = detailList.stream().peek(detailDTO -> {
                detailDTO.setPcs(detailDTO.getPayPcs());
                detailDTO.setWeight(detailDTO.getPayWeight());
                detailDTO.setVolume(detailDTO.getPayVolume());
            }).collect(Collectors.toList());
        }
        return detailList;
    }
}