package com.hzs.system.config.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EMenu;
import com.hzs.common.core.enums.ENoticeFunctionType;
import com.hzs.common.core.enums.ENoticeType;
import com.hzs.common.core.enums.ENoticeWebsiteType;
import com.hzs.common.domain.system.config.BdNoticeConfig;
import com.hzs.common.domain.system.sys.SysRole;
import com.hzs.system.config.mapper.BdNoticeConfigMapper;
import com.hzs.system.config.param.BdNoticeConfigParam;
import com.hzs.system.config.service.IBdNoticeConfigService;
import com.hzs.system.config.vo.BdNoticeConfigSecondCategoryVO;
import com.hzs.system.config.vo.BdNoticeConfigTopCategoryVO;
import com.hzs.system.config.vo.BdNoticeConfigVO;
import com.hzs.system.sys.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 公告配置表 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-02-02
 */
@Service
public class BdNoticeConfigServiceImpl extends ServiceImpl<BdNoticeConfigMapper, BdNoticeConfig> implements IBdNoticeConfigService {

    @Autowired
    ISysUserService sysUserService;


    @Override
    public Boolean saveNoticeConfig(BdNoticeConfigVO noticeConfigVO) {
        return this.saveBatch(gerMemberNoticeConfig(noticeConfigVO));
    }

    @Override
    public Boolean saveAdminNoticeConfig(List<BdNoticeConfigSecondCategoryVO> secondCategoryVOList, Integer pkCountry, Long pkMember) {
        return this.saveBatch(gerMemberNoticeConfig(secondCategoryVOList, pkCountry, pkMember));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateMemberNoticeConfig(BdNoticeConfigVO noticeConfigVO) {
        baseMapper.updateNoticeConfig(noticeConfigVO);
        return this.saveBatch(gerMemberNoticeConfig(noticeConfigVO));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAdminNoticeConfig(List<BdNoticeConfigSecondCategoryVO> secondCategoryVOList, Integer pkCountry, Long pkMember) {
        BdNoticeConfigVO noticeConfigVO = BdNoticeConfigVO.builder().websiteType(ENoticeWebsiteType.ADMIN.getValue())
                .functionType(ENoticeFunctionType.NOTICE.getValue()).pkCountry(pkCountry).pkMember(pkMember).build();
        baseMapper.updateNoticeConfig(noticeConfigVO);
        return this.saveBatch(gerMemberNoticeConfig(secondCategoryVOList, pkCountry, pkMember));
    }

    /**
     * 获取前台公告、站内信配置
     *
     * @param noticeConfigVO 公告配置信息
     * @return List<BdNoticeConfig>
     */
    private List<BdNoticeConfig> gerMemberNoticeConfig(BdNoticeConfigVO noticeConfigVO) {
        BdNoticeConfig noticeConfig;
        List<BdNoticeConfig> bdNoticeConfigs = new ArrayList<>();
        List<BdNoticeConfigTopCategoryVO> topCategoryVOList = noticeConfigVO.getTopCategoryVOList();
        if (CollUtil.isNotEmpty(topCategoryVOList)) {
            for (BdNoticeConfigTopCategoryVO topCategoryVO : topCategoryVOList) {
                Integer topCateGroy = topCategoryVO.getTopCategory();
                //二级分类
                List<BdNoticeConfigSecondCategoryVO> secondCategoryVOList = topCategoryVO.getSecondCategoryVOList();
                if (CollUtil.isNotEmpty(secondCategoryVOList)) {
                    for (BdNoticeConfigSecondCategoryVO secondCategoryVO : secondCategoryVOList) {
                        Integer secondCategroy = secondCategoryVO.getSecondCategory();
                        //内容
                        List<Integer> contentVOList = secondCategoryVO.getContentVOList();
                        //首页
                        if(topCateGroy.equals(ENoticeType.MENU.getValue())&&secondCategroy.equals(EMenu.INDEX.getValue())){
                            contentVOList = new ArrayList<>();
                            contentVOList.add(EMenu.INDEX.getValue());
                        }
                        if (CollUtil.isNotEmpty(contentVOList)) {
                            for (Integer content : contentVOList) {
                                noticeConfig = new BdNoticeConfig();
                                noticeConfig.setFunctionType(noticeConfigVO.getFunctionType());
                                noticeConfig.setWebsiteType(noticeConfigVO.getWebsiteType());
                                noticeConfig.setTopCategory(topCateGroy);
                                noticeConfig.setSecondCategory(secondCategroy);
                                noticeConfig.setContent(content);
                                noticeConfig.setPkCreator(noticeConfigVO.getPkMember());
                                noticeConfig.setCreationTime(new Date());
                                noticeConfig.setPkCountry(noticeConfigVO.getPkCountry());
                                bdNoticeConfigs.add(noticeConfig);
                            }
                        }
                    }
                }
            }
        }
        return bdNoticeConfigs;
    }

    /**
     * 获取后台公告配置信息
     *
     * @param secondCategoryVOList 配置信息
     * @param pkCountry            国家ID
     * @param pkMember             会员ID
     * @return List<BdNoticeConfig>
     */

    private List<BdNoticeConfig> gerMemberNoticeConfig(List<BdNoticeConfigSecondCategoryVO> secondCategoryVOList, Integer pkCountry, Long pkMember) {
        BdNoticeConfig noticeConfig;
        List<BdNoticeConfig> bdNoticeConfigs = new ArrayList<>();
        for (BdNoticeConfigSecondCategoryVO secondCategoryVO : secondCategoryVOList) {
            Integer secondCategroy = secondCategoryVO.getSecondCategory();
            //内容
            List<Integer> contentVOList = secondCategoryVO.getContentVOList();
            if (CollUtil.isNotEmpty(contentVOList)) {
                for (Integer content : contentVOList) {
                    noticeConfig = new BdNoticeConfig();
                    noticeConfig.setFunctionType(ENoticeFunctionType.NOTICE.getValue());
                    noticeConfig.setWebsiteType(ENoticeWebsiteType.ADMIN.getValue());
                    noticeConfig.setSecondCategory(secondCategroy);
                    noticeConfig.setContent(content);
                    noticeConfig.setPkCreator(pkMember);
                    noticeConfig.setCreationTime(new Date());
                    noticeConfig.setPkCountry(pkCountry);
                    bdNoticeConfigs.add(noticeConfig);
                }
            }
        }
        return bdNoticeConfigs;
    }


    @Override
    public List<BdNoticeConfigTopCategoryVO> getNoticeConfig(BdNoticeConfigVO noticeConfigVO) {
        List<Integer> contentList;
        Map<Integer, Map<Integer, List<Integer>>> noticeConfigMap = new HashMap<>();
        //二级分类
        Map<Integer, List<Integer>> secondConfigMap;
        List<BdNoticeConfig> bdNoticeConfigList = baseMapper.getBdNoticeConfigList(noticeConfigVO);
        if (CollUtil.isNotEmpty(bdNoticeConfigList)) {
            for (BdNoticeConfig bdNoticeConfig : bdNoticeConfigList) {
                Integer topCategory = bdNoticeConfig.getTopCategory();
                if (MapUtil.isEmpty(noticeConfigMap.get(topCategory))) {
                    secondConfigMap = new HashMap<>();
                    noticeConfigMap.put(topCategory, secondConfigMap);
                } else {
                    secondConfigMap = noticeConfigMap.get(topCategory);
                }
                Integer secondCategory = bdNoticeConfig.getSecondCategory();
                if (CollUtil.isEmpty(noticeConfigMap.get(topCategory).get(secondCategory))) {
                    contentList = new ArrayList<>();
                } else {
                    contentList = noticeConfigMap.get(topCategory).get(secondCategory);
                }

                Integer content = bdNoticeConfig.getContent();
                contentList.add(content);

                secondConfigMap.put(secondCategory, contentList);
                noticeConfigMap.put(topCategory, secondConfigMap);
            }
        }
        List<BdNoticeConfigTopCategoryVO> list = mapToList(noticeConfigMap);
        return CollUtil.isEmpty(list) ? new ArrayList<>() : list;
    }

    /**
     * map 转 list
     *
     * @param map map
     * @return List<BdNoticeConfigTopCategoryVO>
     */
    private List<BdNoticeConfigTopCategoryVO> mapToList(Map<Integer, Map<Integer, List<Integer>>> map) {
        BdNoticeConfigSecondCategoryVO secondCategoryVO;
        BdNoticeConfigTopCategoryVO topCategoryVO;
        List<BdNoticeConfigTopCategoryVO> topCategoryVOList = new ArrayList<>();
        for (Map.Entry<Integer, Map<Integer, List<Integer>>> entry : map.entrySet()) {
            Integer topCategory = entry.getKey();
            Map<Integer, List<Integer>> secondConfigMap = entry.getValue();
            if (MapUtil.isNotEmpty(secondConfigMap)) {
                List<BdNoticeConfigSecondCategoryVO> secondCategoryVOList = new ArrayList<>();
                for (Map.Entry<Integer, List<Integer>> secondCategory : secondConfigMap.entrySet()) {
                    Integer key = secondCategory.getKey();
                    List<Integer> value = secondCategory.getValue();
                    secondCategoryVO = new BdNoticeConfigSecondCategoryVO();
                    secondCategoryVO.setSecondCategory(key);
                    secondCategoryVO.setContentVOList(value);
                    secondCategoryVOList.add(secondCategoryVO);
                }
                topCategoryVO = new BdNoticeConfigTopCategoryVO();
                topCategoryVO.setTopCategory(topCategory);

                topCategoryVO.setSecondCategoryVOList(secondCategoryVOList);
                topCategoryVOList.add(topCategoryVO);
            }

        }
        return topCategoryVOList;

    }


    @Override
    public List<BdNoticeConfigSecondCategoryVO> getAdminNoticeConfig(Integer pkCountry) {
        List<Integer> contentList;
        //二级分类
        Map<Integer, List<Integer>> secondConfigMap = new HashMap<>();
        BdNoticeConfigVO noticeConfigVO = BdNoticeConfigVO.builder().pkCountry(pkCountry)
                .websiteType(ENoticeWebsiteType.ADMIN.getValue())
                .functionType(ENoticeFunctionType.NOTICE.getValue()).build();
        //根据条件查询数据
        List<BdNoticeConfig> bdNoticeConfigList = baseMapper.getBdNoticeConfigList(noticeConfigVO);
        if (CollUtil.isNotEmpty(bdNoticeConfigList)) {
            for (BdNoticeConfig bdNoticeConfig : bdNoticeConfigList) {
                Integer secondCategory = bdNoticeConfig.getSecondCategory();
                if (CollUtil.isEmpty(secondConfigMap.get(secondCategory))) {
                    contentList = new ArrayList<>();
                } else {
                    contentList = secondConfigMap.get(secondCategory);
                }
                Integer content = bdNoticeConfig.getContent();
                contentList.add(content);

                secondConfigMap.put(secondCategory, contentList);
            }
        }
        List<BdNoticeConfigSecondCategoryVO> list = adminMapToList(secondConfigMap);
        return CollUtil.isEmpty(list) ? new ArrayList<>() : list;
    }

    /**
     * 后台配置map转list
     *
     * @param secondConfigMap msp
     * @return
     */
    private List<BdNoticeConfigSecondCategoryVO> adminMapToList(Map<Integer, List<Integer>> secondConfigMap) {
        BdNoticeConfigSecondCategoryVO secondCategoryVO;
        List<BdNoticeConfigSecondCategoryVO> secondCategoryVOList = new ArrayList<>();
        if (MapUtil.isNotEmpty(secondConfigMap)) {
            for (Map.Entry<Integer, List<Integer>> entry : secondConfigMap.entrySet()) {
                Integer key = entry.getKey();
                List<Integer> value = entry.getValue();
                secondCategoryVO = new BdNoticeConfigSecondCategoryVO();
                secondCategoryVO.setSecondCategory(key);
                secondCategoryVO.setContentVOList(value);
                secondCategoryVOList.add(secondCategoryVO);
            }

        }
        return secondCategoryVOList;
    }

    @Override
    public List<SysRole> showAdminList(Integer pkCountry) {
        return sysUserService.selectSysUserRole(pkCountry);
    }

    @Override
    public Integer updateNoticeConfigByContent(BdNoticeConfigVO noticeConfigVO) {
       return  baseMapper.updateNoticeConfigByContent(noticeConfigVO);
    }

    @Override
    public String getBdNoticeConfigListByCategroy(BdNoticeConfigParam param){
        return baseMapper.getBdNoticeConfigListByCategroy(param);
    }

    @Override
    public List<Map<String, String>> getNoticeRoleAndUser(BdNoticeConfigParam param){
        return baseMapper.getNoticeRoleAndUser(param);
    }
}
