package com.prolog.rdc.tms.controller;

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.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.BmsShipBillImportVO;
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 org.springframework.web.multipart.MultipartFile;

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

/**
 * @author hongbo.pan
 * @date 2023/4/24
 */
@RestController
@Api(tags = "BMS-收入运单管理")
@RequestMapping("/bmsShipBill")
public class BmsShipBillController {

    @Autowired
    private BmsShipBillService service;

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

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

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

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

    @UserLog("运单更新")
    @ApiOperation(value = "运单更新", notes = "运单更新")
    @PostMapping("/update")
    public RestMessage<Object> update(@RequestBody @Valid BmsShipBillVO vo) {
        vo.setBusinessTypeHidden(BusinessTypeEnum.INCOME.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.INCOME.getCode());
        service.saveAndCal(vo,"2");
        return RestMessage.newInstance(true, "操作成功", null);
    }

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

    @UserLog("运单复制核算")
    @ApiOperation(value = "运单复制核算", notes = "运单复制核算")
    @PostMapping("/copyAndCal")
    public RestMessage<Object> copyAndCal(@RequestBody @Valid BmsShipBillVO vo) {
        vo.setBusinessTypeHidden(BusinessTypeEnum.INCOME.getCode());
        service.saveAndCal(vo,"3");
        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("/download_template")
    public RestMessage<String> shipImport(HttpServletResponse response) throws IOException {
        ExcelUtils.export(response, BmsShipBillImportVO.class, "运单模板", null, null, null, true);
        return null;
    }

    @UserLog("运单导入")
    @ApiOperation(value = "运单导入", notes = "运单导入")
    @PostMapping("/import")
    public RestMessage<String> shipImport(@RequestParam("file") MultipartFile file) {
        service.importShipbillData(file);
        return RestMessage.newInstance(true, "导入成功", null);
    }

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

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

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

}