package com.hzs.sale.deliver.controller.manage;

import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.annotation.RepeatSubmitSimple;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.constant.msg.SaOrderMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.sale.deliver.SaDeliver;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.common.util.TransactionUtils;
import com.hzs.sale.deliver.param.DeliverConfirmParam;
import com.hzs.sale.deliver.param.DeliverTemplateParam;
import com.hzs.sale.deliver.param.DeliverParam;
import com.hzs.sale.deliver.service.ISaDeliverService;
import com.hzs.sale.deliver.vo.DeliverTemplateVO;
import com.hzs.sale.deliver.vo.DeliverVO;
import com.hzs.sale.util.OrderUtil;
import com.hzs.system.base.IAreaServiceApi;
import com.hzs.system.sys.IUserServiceApi;
import com.hzs.system.sys.dto.UserAuthorityDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 发货清单立即发货控制器
 */
@RestController
@RequestMapping("/manage/deliver")
public class SaDeliverController extends BaseController {

    @Autowired
    private ISaDeliverService iSaDeliverService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;
    @Autowired
    private UserTokenService userTokenService;

    @DubboReference
    IAreaServiceApi iAreaServiceApi;
    @DubboReference
    IUserServiceApi iUserServiceApi;

    /**
     * 列表
     *
     * @param param 立即发货列表查询入参
     * @return
     */
    @AccessPermissions("DeliveryImmediate")
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.IMMEDIATE_DELIVER, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(DeliverParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        DateUtils.handTimeStartAndEnd(param.getPayTime());
        DateUtils.handTimeStartAndEnd(param.getPrintTime());
        DateUtils.handTimeStartAndEnd(param.getDeliverTime());
        // 系统类型
        param.setSystemType(SecurityUtils.getSystemType());
        // 立即发货列表只显示已打印的发货单
        param.setPrintStatus(EPrintStatus.PRINT.getValue());

        // 获取管理员权限（角色地区范围、体系列表、团队列表）
        UserAuthorityDTO userAuthorityDTO = iUserServiceApi.getUserAuthority(SecurityUtils.getUserId()).getData();
        param.setAreaScopeList(userAuthorityDTO.getRoleAreaScopeList());
        param.setVertexIdList(userAuthorityDTO.getVertexIdList());
        param.setTeamList(userAuthorityDTO.getUserTeamList());

        Integer pkCountry = SecurityUtils.getPkCountry();
        startPage();
        List<DeliverVO> resultList = iSaDeliverService.deliverList(param, pkCountry);

        // 当查询有数据才进行处理
        if (resultList.size() > 0) {
            // 获取行政区划map
            Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(pkCountry).getData();

            for (DeliverVO deliver : resultList) {
                // 省市区处理
                OrderUtil.handleOrderAddress(areaMap, deliver);
                // 订单类型枚举转换
                deliver.setOrderTypeVal(EOrderType.getLabelByValue(deliver.getOrderType()));
                // 发货类型
                deliver.setShippingChannelVal(EShippingChannel.getLabelByValue(deliver.getShippingChannel()));
                // 发货方式
                deliver.setDeliveryWayVal(EDelivery.getLabelByValue(deliver.getDeliveryWay()));
                // 供应方式
                deliver.setOperateScopeVal(ESupplyWay.getLabelByVal(deliver.getOperateScope()));
                // 运输方式
                deliver.setTranTypeVal(ETransportType.getLabelByValue(deliver.getTranType()));
                // 发货状态
                deliver.setDeliverStatusVal(EDeliveryStatus.getEnumByValue(deliver.getDeliverStatus()).getLabel());
            }
        }
        return getDataTable(resultList);
    }


    /**
     * 导出
     *
     * @param response
     * @param param    立即发货列表查询入参
     */
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.IMMEDIATE_DELIVER, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeliverParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        DateUtils.handTimeStartAndEnd(param.getPayTime());
        DateUtils.handTimeStartAndEnd(param.getPrintTime());
        DateUtils.handTimeStartAndEnd(param.getDeliverTime());
        // 系统类型
        param.setSystemType(SecurityUtils.getSystemType());

        // 立即发货列表只显示已打印的发货单
        param.setPrintStatus(EPrintStatus.PRINT.getValue());

        // 获取管理员权限（角色地区范围、体系列表、团队列表）
        UserAuthorityDTO userAuthorityDTO = iUserServiceApi.getUserAuthority(SecurityUtils.getUserId()).getData();
        param.setAreaScopeList(userAuthorityDTO.getRoleAreaScopeList());
        param.setVertexIdList(userAuthorityDTO.getVertexIdList());
        param.setTeamList(userAuthorityDTO.getUserTeamList());

        // 国家ID
        Integer pkCountry = SecurityUtils.getPkCountry();
        List<DeliverVO> resultList = iSaDeliverService.deliverList(param, pkCountry);

        // 当查询有数据才进行处理
        if (resultList.size() > 0) {
            // 获取行政区划map
            Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(pkCountry).getData();

            // 获取需要翻译的枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EDeliveryStatus.values(), EDelivery.values(),
                    ESupplyWay.values(), ETransportType.values(), EOrderType.values(), EDealerYesNo.values(), EShippingChannel.values());

            for (DeliverVO deliver : resultList) {
                // 省市区处理
                OrderUtil.handleOrderAddress(areaMap, deliver);

                deliver.setDeliverStatusVal(EDeliveryStatus.getEnumByValue(deliver.getDeliverStatus()).getLabel());
                deliver.setDeliveryWayVal(EDelivery.getLabelByValue(deliver.getDeliveryWay()));
                deliver.setOperateScopeVal(ESupplyWay.getLabelByVal(deliver.getOperateScope()));
                deliver.setOrderTypeVal(EOrderType.getLabelByValue(deliver.getOrderType()));
                deliver.setTranTypeVal(ETransportType.getLabelByValue(deliver.getTranType()));
                deliver.setShippingChannelVal(EShippingChannel.getLabelByValue(deliver.getShippingChannel()));
            }
        }
        ExcelUtil<DeliverVO> util = new ExcelUtil<>(DeliverVO.class);
        util.exportExcel(response, resultList, "发货清单立即发货导出");
    }


    /**
     * 立即发货数据
     *
     * @param param 立即发货入参
     * @return
     */
    @GetMapping("/immediate-data")
    public AjaxResult batchDeliverData(DeliverParam param) {
        // 立即发货列表只显示已打印的发货单
        param.setPrintStatus(EPrintStatus.PRINT.getValue());

        // 国家ID
        Integer pkCountry = SecurityUtils.getPkCountry();

        List<DeliverVO> resultList = iSaDeliverService.deliverList(param, pkCountry);

        if (resultList.size() > 0) {
            // 获取行政区划map
            Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(pkCountry).getData();

            for (DeliverVO deliverVO : resultList) {
                if (EDeliveryStatus.NOT_DELIVERY.getValue() != deliverVO.getDeliverStatus()) {
                    // 发货单已发货
                    return AjaxResult.error(deliverVO.getDeliverCode() + TransactionUtils.getContent(SaOrderMsgConstants.DELIVER_SHIPPED));
                }

                // 省市区处理
                OrderUtil.handleOrderAddress(areaMap, deliverVO);
            }
        }

        return AjaxResult.success(resultList);
    }


    /**
     * 模板下载
     *
     * @param response
     */
    @PostMapping("/template-export")
    public void batchDeliverExport(HttpServletResponse response) {
        ExcelUtil<DeliverTemplateVO> util = new ExcelUtil<>(DeliverTemplateVO.class);
        util.exportExcel(response, null, "物流模板");
    }


    /**
     * 立即发货导入
     *
     * @param file   导入文件
     * @param idList 发货单ID列表
     * @return
     * @throws Exception
     */
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.IMMEDIATE_DELIVER, method = EOperationMethod.IMPORT)
    @PostMapping("/immediate-import")
    public AjaxResult batchDeliverImport(MultipartFile file, @RequestParam("idList") List<Long> idList) throws Exception {
        ExcelUtil<DeliverTemplateParam> util = new ExcelUtil<>(DeliverTemplateParam.class);
        List<DeliverTemplateParam> deliverTemplateParamList = util.importExcel(file.getInputStream());

        // 返回数据
        List<DeliverTemplateVO> resultList = new ArrayList<>(idList.size());

        // 查询发货单信息
        List<SaDeliver> saDeliverList = iSaDeliverService.listByIds(idList);

        for (SaDeliver saDeliver : saDeliverList) {
            // 遍历导入数据，匹配发货单发货数据，如果相同则封装返回发货信息
            for (DeliverTemplateParam deliverTemplate : deliverTemplateParamList) {
                if (saDeliver.getDeliverCode().equals(deliverTemplate.getDeliverCode())) {
                    // 物流单号相同，进行返回
                    resultList.add(DeliverTemplateVO.builder()
                            .pkId(saDeliver.getPkId())
                            .logisticsCompany(deliverTemplate.getLogisticsCompany().trim())
                            .logisticsCode(deliverTemplate.getLogisticsCode().trim())
                            .build());
                    break;
                }
            }
        }
        return AjaxResult.success(resultList);
    }


    /**
     * 确认发货
     *
     * @return
     */
    @RepeatSubmitSimple
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.IMMEDIATE_DELIVER, method = EOperationMethod.CONFIRM, remark = "确认发货")
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody List<DeliverConfirmParam> paramList) {
        if (CollectionUtils.isEmpty(paramList)) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        // 入库操作数据
        List<SaDeliver> deliverList = new ArrayList<>(paramList.size());
        // 发货单ID列表
        List<Long> deliverIdList = new ArrayList<>(paramList.size());

        // 当前用户ID
        Long userId = SecurityUtils.getUserId();
        // 国家ID
        Integer pkCountry = SecurityUtils.getPkCountry();

        for (DeliverConfirmParam deliverConfirmParam : paramList) {
            SaDeliver saDeliver = new SaDeliver();
            if (null == deliverConfirmParam.getPkId() || StringUtils.isAnyEmpty(deliverConfirmParam.getLogisticsCompany(), deliverConfirmParam.getLogisticsCode())) {
                continue;
            }

            saDeliver.setPkId(deliverConfirmParam.getPkId());
            saDeliver.setLogisticsCompany(deliverConfirmParam.getLogisticsCompany());
            saDeliver.setLogisticsCode(deliverConfirmParam.getLogisticsCode().trim().replaceAll("，", ","));
            saDeliver.setDeliverStatus(EDeliveryStatus.DELIVERED.getValue());
            deliverList.add(saDeliver);

            deliverIdList.add(deliverConfirmParam.getPkId());
        }
        if (deliverList.size() == 0) {
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        return toAjax(iSaDeliverService.batchDeliver(deliverList, deliverIdList, userId, pkCountry, userTokenService.getLoginUser()));
    }

    /**
     * 修改单号数据
     *
     * @param deliverId 发货单ID
     * @return
     */
    @GetMapping("/change-code-data/{deliverId}")
    public AjaxResult changeCodeData(@PathVariable("deliverId") Long deliverId) {
        DeliverVO deliverVO = new DeliverVO();

        // 国家ID
        Integer pkCountry = SecurityUtils.getPkCountry();

        List<DeliverVO> resultList = iSaDeliverService.deliverList(DeliverParam.builder()
                .deliverIdList(Collections.singletonList(deliverId))
                .build(), pkCountry);
        if (resultList.size() > 0) {
            // 获取行政区划map
            Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(pkCountry).getData();

            deliverVO = resultList.get(0);

            if (EDeliveryStatus.DELIVERED.getValue() != deliverVO.getDeliverStatus()) {
                // 发货单未发货或已收货，不能修改物流单号
                return AjaxResult.error("发货单未发货或已收货，不能修改物流单号");
            }
            // 省市区处理
            OrderUtil.handleOrderAddress(areaMap, deliverVO);
        }

        return AjaxResult.success(deliverVO);
    }

    /**
     * 修改单号
     *
     * @param param 修改数据
     * @return
     */
    @RepeatSubmitSimple
    @Log(module = EOperationModule.DELIVER_ORDER, business = EOperationBusiness.IMMEDIATE_DELIVER, method = EOperationMethod.CONFIRM, remark = "修改单号")
    @PostMapping("/change-code")
    public AjaxResult changeCode(@RequestBody DeliverConfirmParam param) {
        if (null == param.getPkId() || StringUtils.isAnyEmpty(param.getLogisticsCode(), param.getLogisticsCompany())) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        return toAjax(iSaDeliverService.simpleDeliver(param, SecurityUtils.getUserId(), SecurityUtils.getPkCountry()));
    }

}
