package com.prolog.rdc.tms.controller;

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.product.core.tools.util.JsonMapper;
import com.prolog.rdc.tms.model.dto.ExpensePopDTO;
import com.prolog.rdc.tms.model.dto.FreightPriceDTO;
import com.prolog.rdc.tms.model.dto.FreightPriceExportDTO;
import com.prolog.rdc.tms.model.dto.FreightPriceQuery;
import com.prolog.rdc.tms.model.dto.basicData.BaseFeeDTO;
import com.prolog.rdc.tms.model.vo.*;
import com.prolog.rdc.tms.service.FreightPriceService;
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.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
@Api(tags = "运费价格")
@RequestMapping("/freightPrice")
public class FreightPriceController {

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

    @Autowired
    private FreightPriceService service;

    @UserLog("运费价格-新增")
    @ApiOperation(value = "运费价格-新增", notes = "运费价格-新增")
    @PostMapping("/save")
    public RestMessage<Object> save(@RequestBody @Valid FreightPriceVO vo) {
        logger.info("- > save vo={}", JsonMapper.toJsonString(vo));
        service.save(vo);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运费价格-列表查询")
    @ApiOperation(value = "运费价格-列表查询", notes = "运费价格-列表查询")
    @PostMapping("/findList")
    public RestMessage<Page<FreightPriceDTO>> findList(@RequestBody FreightPriceQuery vo) {
        logger.info("- > findList vo={}", JsonMapper.toJsonString(vo));
        Page<FreightPriceDTO> list = service.findList(vo);
        return RestMessage.newInstance(true, "操作成功", list);
    }

    @UserLog("运费价格-详情")
    @ApiOperation(value = "运费价格-详情", notes = "运费价格-详情")
    @PostMapping("/detail")
    public RestMessage<FreightPriceDTO> detail(@RequestParam("id") Long id) {
        logger.info("- > detail vo={}", JsonMapper.toJsonString(id));
        FreightPriceDTO detail = service.detail(id);
        return RestMessage.newInstance(true, "操作成功", detail);
    }

    @UserLog("运费价格-修改")
    @ApiOperation(value = "运费价格-修改", notes = "运费价格-修改")
    @PostMapping("/update")
    public RestMessage<Object> update(@RequestBody @Valid FreightPriceVO vo) {
        logger.info("- > update vo={}", JsonMapper.toJsonString(vo));
        service.update(vo);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运费价格-启用/停用")
    @ApiOperation(value = "运费价格-启用/停用", notes = "运费价格-启用/停用")
    @PostMapping("/updateStatus")
    public RestMessage<Object> updateStatus(@RequestBody List<StatusUpdate> list) {
        logger.info("- > updateStatus vo={}", JsonMapper.toJsonString(list));
        service.updateStatus(list);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运费价格-删除")
    @ApiOperation(value = "运费价格-删除", notes = "运费价格-删除")
    @PostMapping("/delete")
    public RestMessage<Object> delete(@RequestBody List<StatusUpdate> list) {
        logger.info("- > updateStatus vo={}", JsonMapper.toJsonString(list));
        service.delete(list);
        return RestMessage.newInstance(true, "操作成功", null);
    }

    @UserLog("运费价格-费用管理pop")
    @ApiOperation(value = "运费价格-费用管理pop", notes = "运费价格-费用管理pop")
    @PostMapping("/expensePop")
    public RestMessage<Page<BaseFeeDTO>> expensePop(@RequestBody ExpensePopDTO carrierCode) {
        logger.info("- > expensePop vo={}", JsonMapper.toJsonString(carrierCode));
        Page<BaseFeeDTO> list = service.expensePop(carrierCode);
        return RestMessage.newInstance(true, "操作成功", list);
    }

    @UserLog("运费价格-导入模版下载")
    @ApiOperation(value = "运费价格-导入模版下载", notes = "运费价格-导入模版下载")
    @PostMapping("/download_template")
    public RestMessage<String> shipImport(HttpServletResponse response) throws IOException {
        ExcelUtils.export(response, FreightPriceImportVO.class, "运费价格模板", null, null, null, true);
        return null;
    }

    @UserLog("运费价格-导入")
    @ApiOperation(value = "运费价格-导入", notes = "运费价格-导入")
    @PostMapping("/import")
    public RestMessage<String> priceImport(@RequestParam("file") MultipartFile file){
        service.importFreightPriceData(file);
        return RestMessage.newInstance(true, "导入成功", null);
    }

    @UserLog("导出所有数据")
    @ApiOperation(value = "导出所有数据", notes = "导出所有数据")
    @PostMapping("/export_all")
    public RestMessage<Object> exportAllPriceExecute(HttpServletResponse response, @RequestBody FreightPriceExportVO vo) {
        try {
            List<FreightPriceExportDTO> list = service.exportAllPriceData(vo);
            ExcelUtils.export(response, FreightPriceExportDTO.class, DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN), list);
            return RestMessage.newInstance(true, "导出成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
    }

    @UserLog("导出当前页数据")
    @ApiOperation(value = "导出当前页数据", notes = "导出当前数据")
    @PostMapping("/export_page")
    public RestMessage<Object> exportPagePriceExecute(HttpServletResponse response, @RequestBody FreightPriceExportVO vo) {
        try {
            List<FreightPriceExportDTO> list = service.exportPagePriceData(vo);
            ExcelUtils.export(response, FreightPriceExportDTO.class, DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN), list);
            return RestMessage.newInstance(true, "导出成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
    }

    @UserLog("导出选中数据")
    @ApiOperation(value = "导出选中数据", notes = "导出选中数据")
    @PostMapping("/export_choose")
    public RestMessage<Object> exportChoosePriceExecute(HttpServletResponse response, @RequestBody List<String> ids) {
        try {
            List<FreightPriceExportDTO> list = service.exportChoosePriceData(ids);
            ExcelUtils.export(response, FreightPriceExportDTO.class, DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN), list);
            return RestMessage.newInstance(true, "导出成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
    }
}