package cn.newfeifan.mall.module.trade.controller.app.delivery;

import cn.newfeifan.mall.framework.common.enums.CommonStatusEnum;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;
import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import cn.newfeifan.mall.module.trade.controller.admin.delivery.vo.express.DeliveryExpressExcelVO;
import cn.newfeifan.mall.module.trade.controller.admin.delivery.vo.express.DeliveryExpressImportRespVO;
import cn.newfeifan.mall.module.trade.controller.app.delivery.vo.express.AppDeliveryExpressRespVO;
import cn.newfeifan.mall.module.trade.convert.delivery.DeliveryExpressConvert;
import cn.newfeifan.mall.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import cn.newfeifan.mall.module.trade.service.delivery.DeliveryExpressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;

import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "用户 App - 快递公司")
@RestController
@RequestMapping("/trade/delivery/express")
@Validated
public class AppDeliverExpressController {

    @Resource
    private DeliveryExpressService deliveryExpressService;

    @GetMapping("/list")
    @Operation(summary = "获得快递公司列表")
    public CommonResult<List<AppDeliveryExpressRespVO>> getDeliveryExpressList() {
        List<DeliveryExpressDO> list = deliveryExpressService.getDeliveryExpressListByStatus(CommonStatusEnum.ENABLE.getStatus());
        list.sort(Comparator.comparing(DeliveryExpressDO::getSort));
        return success(DeliveryExpressConvert.INSTANCE.convertList03(list));
    }

    @PostMapping("/import-excel")
    @Operation(summary = "导入快递公司 Excel")
    @OperateLog(type = EXPORT)
    public CommonResult<DeliveryExpressImportRespVO> importDeliveryExpressExcel(@RequestParam("file") MultipartFile file) throws IOException {
        List<DeliveryExpressExcelVO> list = ExcelUtils.read(file, DeliveryExpressExcelVO.class);
        return success(deliveryExpressService.importDeliveryExpressList(list));
    }
}
