package com.hzs.activity.other.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.activity.base.vo.CarActivitVO;
import com.hzs.activity.base.vo.DeptMemberVO;
import com.hzs.activity.base.vo.TourismActivitVO;
import com.hzs.activity.other.service.IAcOtherPriceDataService;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.enums.EActType;
import com.hzs.common.core.enums.EStandard;
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.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
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: zhang jing
 * @date: 2024/1/18 11:37
 * @param:
 * @return:
 **/
@RestController
@RequestMapping("/manage/ac-other-price")
public class AcOtherPriceDataController extends BaseController {

    @Autowired
    private IAcOtherPriceDataService iAcOtherPriceDataService;

    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    /**
     * @description: 旅游奖数据查询
     * @author: zhang jing
     * @date: 2024/1/18 14:31
     * @param: [tourismActivitVO]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("tourism-list")
    public TableDataInfo tourismList(TourismActivitVO tourismActivitVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (pkCountry != CountryConstants.CHINA_COUNTRY) {
            tourismActivitVO.setPkCountry(pkCountry);
        } else {
            tourismActivitVO.setPkCountry(tourismActivitVO.getPkCountry());
        }
        tourismActivitVO.setActType(EActType.TOURISM.getValue());

        startPage();
        List<TourismActivitVO> list = iAcOtherPriceDataService.tourismList(tourismActivitVO);
        if (CollectionUtil.isNotEmpty(list)) {
            DeptMemberVO deptMemberVO = new DeptMemberVO();
            deptMemberVO.setActType(EActType.TOURISM.getValue());
            if (pkCountry != CountryConstants.CHINA_COUNTRY) {
                deptMemberVO.setPkCountry(pkCountry);
            }
            List<DeptMemberVO> deptList = iAcOtherPriceDataService.tourismDeptList(deptMemberVO);
            list.forEach(itm -> {
                List<DeptMemberVO> deptSonList = deptList.stream()
                        .filter(itmm -> itm.getPkBaseConfig().equals(itmm.getPkBaseConfig()) && itm.getPkMember().equals(itmm.getPkMember())).sorted(Comparator.comparing(DeptMemberVO::getPlaceDept)).collect(Collectors.toList());
                itm.setDeptList(deptSonList);
            });
        }
        return getDataTable(list);
    }

    /**
     * @description: 车奖数据查询
     * @author: zhang jing
     * @date: 2024/1/18 15:29
     * @param: [tourismActivitVO]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("car-list")
    public TableDataInfo carList(TourismActivitVO tourismActivitVO) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (pkCountry != CountryConstants.CHINA_COUNTRY) {
            tourismActivitVO.setPkCountry(pkCountry);
        } else {
            tourismActivitVO.setPkCountry(tourismActivitVO.getPkCountry());
        }
        tourismActivitVO.setActType(EActType.CAR.getValue());

        startPage();
        List<TourismActivitVO> list = iAcOtherPriceDataService.tourismList(tourismActivitVO);
        if (CollectionUtil.isNotEmpty(list)) {
            DeptMemberVO deptMemberVO = new DeptMemberVO();
            deptMemberVO.setActType(EActType.CAR.getValue());
            if (pkCountry != CountryConstants.CHINA_COUNTRY) {
                deptMemberVO.setPkCountry(pkCountry);
            }
            List<DeptMemberVO> deptList = iAcOtherPriceDataService.tourismDeptList(deptMemberVO);
            list.forEach(itm -> {
                List<DeptMemberVO> deptSonList = deptList.stream()
                        .filter(itmm -> itm.getPkBaseConfig().equals(itmm.getPkBaseConfig()) && itm.getPkMember().equals(itmm.getPkMember())).sorted(Comparator.comparing(DeptMemberVO::getPlaceDept)).collect(Collectors.toList());
                itm.setDeptList(deptSonList);
            });
        }
        return getDataTable(list);
    }


    /**
     * @description: 导出旅游奖数据查询
     * @author: zhang jing
     * @date: 2024/1/18 15:31
     * @param: [tourismActivitVO, response]
     * @return: void
     **/
    @PostMapping("tourism-export")
    public void tourismExport(TourismActivitVO tourismActivitVO, HttpServletResponse response) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (pkCountry != CountryConstants.CHINA_COUNTRY) {
            tourismActivitVO.setPkCountry(pkCountry);
        } else {
            tourismActivitVO.setPkCountry(tourismActivitVO.getPkCountry());
        }
        tourismActivitVO.setActType(EActType.TOURISM.getValue());

        List<TourismActivitVO> list = iAcOtherPriceDataService.tourismList(tourismActivitVO);
        if (CollectionUtil.isNotEmpty(list)) {
            Map<Integer, String> transactionMap1 = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_GRADE,
                    EnumsPrefixConstants.KEY_AWARD);
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EStandard.values());

            Map<String, DeptMemberVO> placeDeptToDeptMemberMap = new HashMap<>();
            DeptMemberVO deptMemberVO = new DeptMemberVO();
            deptMemberVO.setActType(EActType.TOURISM.getValue());
            if (pkCountry != CountryConstants.CHINA_COUNTRY) {
                deptMemberVO.setPkCountry(pkCountry);
            }

            List<DeptMemberVO> deptList = iAcOtherPriceDataService.tourismDeptList(deptMemberVO);
            if (CollectionUtil.isNotEmpty(deptList)) {
                deptList.forEach(itmm -> {
                    placeDeptToDeptMemberMap.put(itmm.getPkMember() + "" + itmm.getPkBaseConfig() + "" + itmm.getPlaceDept(), itmm);
                });
            }

            list.forEach(itm -> {
                setSumPv(itm, placeDeptToDeptMemberMap);
                itm.setStatusVal(transactionMap.get(EnumsPrefixConstants.STANDARD_STATE + itm.getStatus()));
                itm.setPkAwardsVal(transactionMap1.get(itm.getPkAwards()));
                itm.setPkSettleGradeVal(transactionMap1.get(itm.getPkSettleGrade()));
            });
        }
        ExcelUtil<TourismActivitVO> util = new ExcelUtil<>(TourismActivitVO.class);
        util.exportExcel(response, list, "导出旅游奖数据");
    }

    /**
     * @description: 导出车奖数据
     * @author: zhang jing
     * @date: 2024/1/18 15:33
     * @param: [tourismActivitVO, response]
     * @return: void
     **/
    @PostMapping("car-export")
    public void carExport(TourismActivitVO tourismActivitVO, HttpServletResponse response) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (pkCountry != CountryConstants.CHINA_COUNTRY) {
            tourismActivitVO.setPkCountry(pkCountry);
        } else {
            tourismActivitVO.setPkCountry(tourismActivitVO.getPkCountry());
        }
        tourismActivitVO.setActType(EActType.CAR.getValue());

        List<CarActivitVO> carList = new ArrayList<>();
        List<TourismActivitVO> list = iAcOtherPriceDataService.tourismList(tourismActivitVO);
        if (CollectionUtil.isNotEmpty(list)) {
            Map<Integer, String> transactionMap1 = iTransactionCommonService.exportEnumTransactionByDB(EnumsPrefixConstants.KEY_GRADE,
                    EnumsPrefixConstants.KEY_AWARD);
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EStandard.values());

            Map<String, DeptMemberVO> placeDeptToDeptMemberMap = new HashMap<>();
            DeptMemberVO deptMemberVO = new DeptMemberVO();
            deptMemberVO.setActType(EActType.CAR.getValue());
            if (pkCountry != CountryConstants.CHINA_COUNTRY) {
                deptMemberVO.setPkCountry(pkCountry);
            }

            List<DeptMemberVO> deptList = iAcOtherPriceDataService.tourismDeptList(deptMemberVO);
            if (CollectionUtil.isNotEmpty(deptList)) {
                deptList.forEach(itmm -> {
                    placeDeptToDeptMemberMap.put(itmm.getPkMember() + "" + itmm.getPkBaseConfig() + "" + itmm.getPlaceDept(), itmm);
                });
            }

            list.forEach(itm -> {
                setSumPv(itm, placeDeptToDeptMemberMap);
                itm.setStatusVal(transactionMap.get(EnumsPrefixConstants.STANDARD_STATE + itm.getStatus()));
                itm.setPkAwardsVal(transactionMap1.get(itm.getPkAwards()));
                itm.setPkSettleGradeVal(transactionMap1.get(itm.getPkSettleGrade()));
            });
        }
        list.forEach(item -> carList.add(BeanUtil.copyProperties(item, CarActivitVO.class)));
        ExcelUtil<CarActivitVO> util = new ExcelUtil<>(CarActivitVO.class);
        util.exportExcel(response, carList, "导出车奖数据");
    }

    private void setSumPv(TourismActivitVO itm, Map<String, DeptMemberVO> placeDeptToDeptMemberMap) {
        DeptMemberVO deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 1);
        if (deptMemberVO != null) {
            itm.setSumPv1(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 2);
        if (deptMemberVO != null) {
            itm.setSumPv2(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 3);
        if (deptMemberVO != null) {
            itm.setSumPv3(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 4);
        if (deptMemberVO != null) {
            itm.setSumPv4(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 5);
        if (deptMemberVO != null) {
            itm.setSumPv5(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 6);
        if (deptMemberVO != null) {
            itm.setSumPv6(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 7);
        if (deptMemberVO != null) {
            itm.setSumPv7(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 8);
        if (deptMemberVO != null) {
            itm.setSumPv8(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 9);
        if (deptMemberVO != null) {
            itm.setSumPv9(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 10);
        if (deptMemberVO != null) {
            itm.setSumPv10(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 11);
        if (deptMemberVO != null) {
            itm.setSumPv11(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 12);
        if (deptMemberVO != null) {
            itm.setSumPv12(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 13);
        if (deptMemberVO != null) {
            itm.setSumPv13(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 14);
        if (deptMemberVO != null) {
            itm.setSumPv14(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 15);
        if (deptMemberVO != null) {
            itm.setSumPv15(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 16);
        if (deptMemberVO != null) {
            itm.setSumPv16(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 17);
        if (deptMemberVO != null) {
            itm.setSumPv17(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 18);
        if (deptMemberVO != null) {
            itm.setSumPv18(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 19);
        if (deptMemberVO != null) {
            itm.setSumPv19(deptMemberVO.getSumPv());
        }
        deptMemberVO = placeDeptToDeptMemberMap.get(itm.getPkMember() + "" + itm.getPkBaseConfig() + "" + 20);
        if (deptMemberVO != null) {
            itm.setSumPv20(deptMemberVO.getSumPv());
        }
    }


}
