package com.hzs.member.empty.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.empty.ext.CuMemberEmptyDetailExt;
import com.hzs.common.domain.member.empty.ext.CuMemberEmptyProductExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.member.empty.param.MemberEmptyRegisterQueryParam;
import com.hzs.member.empty.service.ICuMemberEmptyProductService;
import com.hzs.member.empty.service.ICuMemberEmptyService;
import com.hzs.member.empty.vo.MemberEmptyRegisterQueryProductVO;
import com.hzs.member.empty.vo.MemberEmptyRegisterQueryVO;
import com.hzs.system.base.IAreaServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Description: 空单注册列表控制器
 * @Author: jiang chao
 * @Time: 2023/7/6 15:06
 * @Classname: CuMemberEmptyRegisterController
 * @PackageName: com.hzs.member.empty.controller.manage
 */
@RequestMapping("/manage/member-empty-register")
@RestController
public class CuMemberEmptyRegisterController extends BaseController {

    @Autowired
    private ICuMemberEmptyService iCuMemberEmptyService;
    @Autowired
    private ICuMemberEmptyProductService iCuMemberEmptyProductService;
    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    @DubboReference
    private IAreaServiceApi iAreaServiceApi;

    /**
     * 查询注册列表
     *
     * @param param 查询入参
     * @return
     */
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER_REGISTER, method = EOperationMethod.SELECT)
    @GetMapping("/register-list")
    public TableDataInfo registerList(MemberEmptyRegisterQueryParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());

        List<MemberEmptyRegisterQueryVO> resultList = new ArrayList<>();

        Integer pkCountry = SecurityUtils.getPkCountry();

        startPage();
        List<CuMemberEmptyDetailExt> list = iCuMemberEmptyService.emptyRegisterList(param, pkCountry);
        if (CollectionUtil.isNotEmpty(list)) {

            // 查询空单批次产品信息
            Map<Long, List<CuMemberEmptyProductExt>> productMap = iCuMemberEmptyProductService.queryProductList(list.stream().map(CuMemberEmptyDetailExt::getPkId).collect(Collectors.toList()));

            for (CuMemberEmptyDetailExt emptyDetailExt : list) {
                MemberEmptyRegisterQueryVO queryVO = BeanUtil.copyProperties(emptyDetailExt, MemberEmptyRegisterQueryVO.class);

                // 产品数据处理
                List<CuMemberEmptyProductExt> emptyProductList = productMap.get(emptyDetailExt.getPkId());
                if (CollectionUtil.isNotEmpty(emptyProductList)) {
                    queryVO.setDetailList(BeanUtil.copyToList(emptyProductList, MemberEmptyRegisterQueryProductVO.class));
                }

                resultList.add(queryVO);
            }
        }

        TableDataInfo tableDataInfo = getDataTable(list);
        tableDataInfo.setRows(resultList);
        return tableDataInfo;
    }

    /**
     * 导出注册列表
     *
     * @param response
     * @param param    查询参数
     */
    @Log(module = EOperationModule.EMPTY_ORDER, business = EOperationBusiness.EMPTY_ORDER_REGISTER, method = EOperationMethod.EXPORT)
    @PostMapping("/register-export")
    public void registerListExport(HttpServletResponse response, MemberEmptyRegisterQueryParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());

        List<MemberEmptyRegisterQueryVO> resultList = new ArrayList<>();

        Integer pkCountry = SecurityUtils.getPkCountry();

        List<CuMemberEmptyDetailExt> list = iCuMemberEmptyService.emptyRegisterList(param, pkCountry);
        if (CollectionUtil.isNotEmpty(list)) {

            // 查询空单批次产品信息
            Map<Long, List<CuMemberEmptyProductExt>> productMap = iCuMemberEmptyProductService.queryProductList(list.stream().map(CuMemberEmptyDetailExt::getPkId).collect(Collectors.toList()));

            // 获取翻译内容
            Map<String, String> cacheMap = iTransactionCommonService.exportEnumTransaction(EPlaceDept.values(), EOrderSource.values(), EOrderType.values(), EDealerYesNo.values());
            Map<Integer, String> dbMap = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_GRADE, EnumsPrefixConstants.KEY_AWARD);

            for (CuMemberEmptyDetailExt emptyDetailExt : list) {
                MemberEmptyRegisterQueryVO queryVO = BeanUtil.copyProperties(emptyDetailExt, MemberEmptyRegisterQueryVO.class);

                queryVO.setPkGradeKeyVal(dbMap.get(queryVO.getPkGradeKey()));
                queryVO.setPkAwardKeyVal(dbMap.get(queryVO.getPkAwardKey()));
                // 国际化处理
                queryVO.setPlaceDeptVal(cacheMap.get(EnumsPrefixConstants.PLACE_DEPT + queryVO.getPlaceDept()));
                queryVO.setOrderSourceVal(cacheMap.get(EnumsPrefixConstants.ORDER_SOURCE + queryVO.getOrderSource()));
                queryVO.setOrderTypeVal(cacheMap.get(EnumsPrefixConstants.ORDER_TYPE + queryVO.getOrderType()));
                queryVO.setSettlementFlagVal(cacheMap.get(EnumsPrefixConstants.DEALER_YES_NO + queryVO.getSettlementFlag()));
                queryVO.setProductFlagVal(cacheMap.get(EnumsPrefixConstants.DEALER_YES_NO + queryVO.getProductFlag()));

                // 产品信息拼接
                StringBuilder productSb = new StringBuilder();
                // 产品信息
                List<CuMemberEmptyProductExt> emptyProductList = productMap.get(emptyDetailExt.getPkId());
                if (CollectionUtil.isNotEmpty(emptyProductList)) {
                    for (CuMemberEmptyProductExt emptyProduct : emptyProductList) {
                        productSb.append(emptyProduct.getProductName())
                                .append("(")
                                .append(emptyProduct.getSpecsName())
                                .append(") * ")
                                .append(emptyProduct.getQuantity())
                                .append("; \n");
                    }
                }
                queryVO.setProductStr(productSb.toString());

                resultList.add(queryVO);
            }
        }

        ExcelUtil<MemberEmptyRegisterQueryVO> util = new ExcelUtil<>(MemberEmptyRegisterQueryVO.class);
        util.exportExcel(response, resultList, "空单注册列表导出");
    }

}
