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.product.core.tools.util.JsonMapper;
import com.prolog.rdc.tms.model.dto.*;
import com.prolog.rdc.tms.model.entity.BmsLogOperationModel;
import com.prolog.rdc.tms.model.entity.BmsShipBillAdjustModel;
import com.prolog.rdc.tms.model.entity.BmsShipBillDetailModel;
import com.prolog.rdc.tms.model.entity.BmsShipBillPackageModel;
import com.prolog.rdc.tms.model.vo.*;
import com.prolog.rdc.tms.service.ShipBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@RestController
@Api(tags = "运单管理")
@RequestMapping("/shipBill")
public class ShipBillController {

    private final Logger logger = LoggerFactory.getLogger(ShipBillController.class);

    @Autowired
    private ShipBillService shipBillService;

    @UserLog("运单管理-新增")
    @ApiOperation(value = "运单管理-新增", notes = "运单管理-新增")
    @PostMapping("/save/v1.0")
    public RestMessage<Object> save(@RequestBody @Valid ShipBillVO vo) {
        logger.info("- > save vo={}", JsonMapper.toJsonString(vo));
        shipBillService.save(vo);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运单管理-列表查询")
    @ApiOperation(value = "运单管理-列表查询", notes = "运单管理-列表查询")
    @PostMapping("/findList/v1.0")
    public RestMessage<Page<ShipBillDTO>> findList(@RequestBody @Valid ShipBillManagementQueryVO vo) throws ParseException {
        logger.info("- > findList vo={}", JsonMapper.toJsonString(vo));
        Page<ShipBillDTO> page = shipBillService.findList(vo);
        return RestMessage.newInstance(true, "操作成功", page);
    }

    @UserLog("运单管理-详情")
    @ApiOperation(value = "运单管理-详情", notes = "运单管理-详情")
    @PostMapping("/detail/v1.0")
    public RestMessage<ShipBillDTO> detail(@RequestParam("id") Long id) {
        logger.info("- > findList vo={}", JsonMapper.toJsonString(id));
        ShipBillDTO detail = shipBillService.detail(id);
        return RestMessage.newInstance(true, "操作成功", detail);
    }

    @UserLog("运单管理-更新")
    @ApiOperation(value = "运单管理-更新", notes = "运单管理-更新")
    @PostMapping("/update/v1.0")
    public RestMessage<Object> update(@RequestBody @Valid ShipBillVO vo) {
        logger.info("- > update vo={}", JsonMapper.toJsonString(vo));
        shipBillService.update(vo);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运单管理-复制")
    @ApiOperation(value = "运单管理-更新", notes = "运单管理-更新")
    @PostMapping("/copy/v1.0")
    public RestMessage<Object> copy(@RequestBody @Valid ShipBillVO vo) {
        logger.info("- > copy vo={}", JsonMapper.toJsonString(vo));
        shipBillService.copy(vo);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运单管理-提交")
    @ApiOperation(value = "运单管理-提交", notes = "运单管理-提交")
    @PostMapping("/submit/v1.0")
    public RestMessage<Object> submit(@RequestBody List<String> ids) {
        logger.info("- > submit vo={}", JsonMapper.toJsonString(ids));
        shipBillService.submit(ids);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运单管理-核算")
    @ApiOperation(value = "运单管理-核算", notes = "运单管理-核算")
    @PostMapping("/adjustAccounts/v1.0")
    public RestMessage<Object> adjustAccounts(@RequestBody List<String> ids) {
        logger.info("- > adjustAccounts vo={}", JsonMapper.toJsonString(ids));
        shipBillService.adjustAccounts(ids);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运单费用管理-提交")
    @ApiOperation(value = "运单费用管理-提交", notes = "运单费用管理-提交")
    @PostMapping("/feeSubmit/v1.0")
    public RestMessage<ShipBillFeeDTO> feeSubmit(@RequestBody List<String> ids) {
        logger.info("- > feeSubmit vo={}", JsonMapper.toJsonString(ids));
        shipBillService.feeSubmit(ids);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("费用信息-提交")
    @ApiOperation(value = "费用信息-提交", notes = "费用信息-提交")
    @PostMapping("/feeInfoSubmit/v1.0")
    public RestMessage<ShipBillFeeDTO> feeInfoSubmit(@RequestBody List<String> ids) {
        logger.info("- > feeSubmit vo={}", JsonMapper.toJsonString(ids));
        shipBillService.feeSubmit(ids);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运单管理-商品")
    @ApiOperation(value = "运单管理-商品", notes = "运单管理-商品")
    @PostMapping("/findListForItem/v1.0")
    public RestMessage<List<BmsShipBillDetailModel>> findListForItem(@RequestParam("id") Long id) {
        logger.info("- > findListForItem vo={}", JsonMapper.toJsonString(id));
        List<BmsShipBillDetailModel> page = shipBillService.findListForItem(id);
        return RestMessage.newInstance(true, "操作成功", page);
    }

    @UserLog("运单管理-包裹")
    @ApiOperation(value = "运单管理-包裹", notes = "运单管理-包裹")
    @PostMapping("/findListForPack/v1.0")
    public RestMessage<List<BmsShipBillPackageModel>> findListForPack(@RequestParam("id") Long id) {
        logger.info("- > findListForPack vo={}", JsonMapper.toJsonString(id));
        List<BmsShipBillPackageModel> page = shipBillService.findListForPack(id);
        return RestMessage.newInstance(true, "操作成功", page);
    }

    @UserLog("运单管理-调整")
    @ApiOperation(value = "运单管理-调整", notes = "运单管理-调整")
    @PostMapping("/findListForAdjust/v1.0")
    public RestMessage<List<BmsShipBillAdjustModel>> findListForAdjust(@RequestParam("id") Long id) {
        logger.info("- > findListForAdjust vo={}", JsonMapper.toJsonString(id));
        List<BmsShipBillAdjustModel> page = shipBillService.findListForAdjust(id);
        return RestMessage.newInstance(true, "操作成功", page);
    }

    @UserLog("运单管理-日志")
    @ApiOperation(value = "运单管理-日志", notes = "运单管理-日志")
    @PostMapping("/findListForTraLog/v1.0")
    public RestMessage<List<BmsLogOperationModel>> findListForTraLog(@RequestParam("id") Long id) {
        logger.info("- > findListForLog vo={}", JsonMapper.toJsonString(id));
        List<BmsLogOperationModel> page = shipBillService.findListForTraLog(id);
        return RestMessage.newInstance(true, "操作成功", page);
    }

    @UserLog("运单管理-日志")
    @ApiOperation(value = "运单管理-日志", notes = "运单管理-日志")
    @PostMapping("/findListForLog/v1.0")
    public RestMessage<List<BmsLogOperationModel>> findListForLog(@RequestParam("id") Long id) {
        logger.info("- > findListForLog vo={}", JsonMapper.toJsonString(id));
        List<BmsLogOperationModel> page = shipBillService.findListForLog(id);
        return RestMessage.newInstance(true, "操作成功", page);
    }

    @UserLog("运单管理-模版下载")
    @ApiOperation(value = "运单管理-导入", notes = "运单管理-导入")
    @PostMapping("/download_template/v1.0")
    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/v1.0")
    public RestMessage<String> shipImport(@RequestParam("file") MultipartFile file, HttpServletRequest httpServletRequest) {
        shipBillService.importShipbillData(file);
        return RestMessage.newInstance(true, "导入成功", null);
    }

    @UserLog("运单费用管理-费用录入")
    @ApiOperation(value = "运单费用管理-费用录入", notes = "运单费用管理-费用录入")
    @PostMapping("/shipBillFeeSave/v1.0")
    public RestMessage<Object> shipBillFeeSave(@RequestBody @Valid ShipBillFeeSaveDTO dto) {
        logger.info("- > shipBillFeeSave vo={}", JsonMapper.toJsonString(dto));
        shipBillService.shipBillFeeSave(dto);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运单费用管理-费用模式查询")
    @ApiOperation(value = "运单费用管理-费用模式查询", notes = "运单费用管理-费用模式查询")
    @PostMapping("/feeList/v1.0")
    public RestMessage<Page<ShipBillFeeListDTO>> feeList(@RequestBody @Valid ShipBillQueryVO vo) {
        logger.info("- > shipBillFeeSave vo={}", JsonMapper.toJsonString(vo));
        Page<ShipBillFeeListDTO> page = shipBillService.feeList(vo);
        return RestMessage.newInstance(true, "操作成功", page);
    }

    @UserLog("运单费用管理-费用模式-查询详情")
    @ApiOperation(value = "运单费用管理-查询详情", notes = "运单费用管理-查询详情")
    @PostMapping("/shipBillFeeDetail/v1.0")
    public RestMessage<ShipBillFeeDTO> shipBillFeeDetail(@RequestParam("id") Long id) {
        logger.info("- > shipBillFeeDetail vo={}", JsonMapper.toJsonString(id));
        ShipBillFeeDTO dto = shipBillService.shipBillFeeDetail(id);
        return RestMessage.newInstance(true, "操作成功", dto);
    }

    @UserLog("运单费用管理-运单模式-详情")
    @ApiOperation(value = "运单费用管理-运单模式-详情", notes = "运单费用管理-运单模式-详情")
    @PostMapping("/shipBillFeeDetailForTran/v1.0")
    public RestMessage<ShipBillFeeDTO> shipBillFeeDetailForTran(@RequestParam("id") Long id,@RequestParam("dataSource") String dataSource) {
        logger.info("- > shipBillFeeDetailForTran vo={}", JsonMapper.toJsonString(id));
        ShipBillFeeDTO dto = shipBillService.shipBillFeeDetailForTran(id,dataSource);
        return RestMessage.newInstance(true, "操作成功", dto);
    }

    @UserLog("运单费用管理-费用查询")
    @ApiOperation(value = "运单费用管理-费用查询", notes = "运单费用管理-费用查询")
    @PostMapping("/queryFeeInfo/v1.0")
    public RestMessage<List<ShipBillFeeDetail>> queryFeeInfo(@RequestParam("id") Long id) {
        logger.info("- > shipBillFeeDetail vo={}", JsonMapper.toJsonString(id));
        List<ShipBillFeeDetail> dto = shipBillService.queryFeeInfo(id);
        return RestMessage.newInstance(true, "操作成功", dto);
    }

    @UserLog("运单费用管理-更新")
    @ApiOperation(value = "运单费用管理-更新", notes = "运单费用管理-更新")
    @PostMapping("/shipBillFeeUpdate/v1.0")
    public RestMessage<Object> shipBillFeeUpdate(@RequestBody @Valid ShipBillFeeSaveDTO dto) {
        logger.info("- > shipBillFeeUpdate vo={}", JsonMapper.toJsonString(dto));
        shipBillService.shipBillFeeUpdate(dto);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运单费用管理-费用管理弹框")
    @ApiOperation(value = "运单费用管理-费用管理弹框", notes = "运单费用管理-费用管理弹框")
    @PostMapping("/feeManagement/v1.0")
    public RestMessage<Page<FeeManagementPopDTO>> feeManagement(@RequestBody @Valid ExpenseManagementQueryVO vo) {
        logger.info("- > feeManagement vo={}", JsonMapper.toJsonString(vo));
        Page<FeeManagementPopDTO> page = shipBillService.feeManagement(vo);
        return RestMessage.newInstance(true, "操作成功", page);
    }

    @UserLog("费用结算-详情")
    @ApiOperation(value = "费用结算-详情", notes = "费用结算-详情")
    @PostMapping("/shipBillDetail/v1.0")
    public RestMessage<ShipBillFeeDTO> shipBillDetail(@RequestParam("shipbillNo") String shipbillNo) {
        logger.info("- > shipBillDetail vo={}", JsonMapper.toJsonString(shipbillNo));
        ShipBillFeeDTO dto = shipBillService.shipBillDetail(shipbillNo);
        return RestMessage.newInstance(true, "操作成功", dto);
    }


    @UserLog("运单管理-录入弹框")
    @ApiOperation(value = "运单管理-录入弹框", notes = "运单管理-录入弹框")
    @PostMapping("/shipbillPop/v1.0")
    public RestMessage<List<ShipBillDTO>> shipbillPop(@RequestBody List<String> shipbillNos) {
        logger.info("- > shipbillPop vo={}", JsonMapper.toJsonString(shipbillNos));
        List<ShipBillDTO> list = shipBillService.shipbillPop(shipbillNos);
        return RestMessage.newInstance(true, "操作成功", list);
    }
}