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

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.EPermissionType;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.system.config.BdMenu;
import com.hzs.common.domain.system.config.BdMenuAuthority;
import com.hzs.common.domain.system.config.BdMenuDetail;
import com.hzs.system.config.mapper.BdMenuDetailMapper;
import com.hzs.system.config.mapper.BdMenuAuthorityMapper;
import com.hzs.system.config.mapper.BdMenuMapper;
import com.hzs.system.config.param.MenuQueryParam;
import com.hzs.system.config.param.MenuSaveParam;
import com.hzs.system.config.service.IBdMenuService;
import com.hzs.system.config.vo.MenuQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 菜单配置表 服务实现类
 *
 * @author hzs
 * @since 2022-10-08
 */
@Service
public class BdMenuServiceImpl extends ServiceImpl<BdMenuMapper, BdMenu> implements IBdMenuService {

    @Autowired
    private BdMenuAuthorityMapper bdMenuAuthorityMapper;
    @Autowired
    private BdMenuDetailMapper bdMenuDetailMapper;

    @Override
    public List<MenuQueryVO> queryList(MenuQueryParam param) {
        return baseMapper.queryList(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveMenu(MenuSaveParam param, Long userId, Integer pkCountry) {
        Date nowDate = new Date();

        BdMenu bdMenu = new BdMenu();
        bdMenu.setPkCountry(pkCountry);
        bdMenu.setPkCreator(userId);
        bdMenu.setCreationTime(nowDate);
        bdMenu.setGradeValues(StringUtils.join(param.getGradeIdList(), ","));
        bdMenu.setAwardValues(StringUtils.join(param.getAwardsIdList(), ","));
        bdMenu.setMenuValues(StringUtils.join(param.getMenuValueList(), ","));
        bdMenu.setAuthorityValues(StringUtils.join(param.getAuthorityIdList(), ","));

        if (baseMapper.insert(bdMenu) > 0) {

            List<BdMenuAuthority> menuAuthoritieList = new ArrayList<>();
            // 等级处理
            for (Integer integer : param.getGradeIdList()) {
                BdMenuAuthority bdMenuAuthority = new BdMenuAuthority();
                bdMenuAuthority.setPkMenu(bdMenu.getPkId());
                bdMenuAuthority.setAuthorityType(EPermissionType.GRADE.getValue());
                bdMenuAuthority.setAuthorityValue(integer);
                bdMenuAuthority.setPkCountry(pkCountry);
                bdMenuAuthority.setPkCreator(userId);
                bdMenuAuthority.setCreationTime(nowDate);
                menuAuthoritieList.add(bdMenuAuthority);
            }
            // 奖衔处理
            for (Integer integer : param.getAwardsIdList()) {
                BdMenuAuthority bdMenuAuthority = new BdMenuAuthority();
                bdMenuAuthority.setPkMenu(bdMenu.getPkId());
                bdMenuAuthority.setAuthorityType(EPermissionType.AWARDS.getValue());
                bdMenuAuthority.setAuthorityValue(integer);
                bdMenuAuthority.setPkCountry(pkCountry);
                bdMenuAuthority.setPkCreator(userId);
                bdMenuAuthority.setCreationTime(nowDate);
                menuAuthoritieList.add(bdMenuAuthority);
            }
            // 权限处理
            for (Integer integer : param.getAuthorityIdList()) {
                BdMenuAuthority bdMenuAuthority = new BdMenuAuthority();
                bdMenuAuthority.setPkMenu(bdMenu.getPkId());
                bdMenuAuthority.setAuthorityType(EPermissionType.AUTHORITY.getValue());
                bdMenuAuthority.setAuthorityValue(integer);
                bdMenuAuthority.setPkCountry(pkCountry);
                bdMenuAuthority.setPkCreator(userId);
                bdMenuAuthority.setCreationTime(nowDate);
                menuAuthoritieList.add(bdMenuAuthority);
            }
            bdMenuAuthorityMapper.insertBatch(menuAuthoritieList);

            List<BdMenuDetail> menuDetailList = new ArrayList<>();
            // 菜单明细项维护
            for (Integer integer : param.getMenuValueList()) {
                BdMenuDetail bdMenuDetail = new BdMenuDetail();
                bdMenuDetail.setPkMenu(bdMenu.getPkId());
                bdMenuDetail.setDetailValue(integer);
                bdMenuDetail.setPkCountry(pkCountry);
                bdMenuDetail.setPkCreator(userId);
                bdMenuDetail.setCreationTime(nowDate);
                menuDetailList.add(bdMenuDetail);
            }
            bdMenuDetailMapper.insertBatch(menuDetailList);

            return true;
        }
        return false;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateMenu(MenuSaveParam param, Long userId, Integer pkCountry) {
        Date nowDate = new Date();

        BdMenu bdMenu = new BdMenu();
        bdMenu.setPkId(param.getMenuId());
        bdMenu.setPkModified(userId);
        bdMenu.setModifiedTime(nowDate);
        bdMenu.setGradeValues(StringUtils.join(param.getGradeIdList(), ","));
        bdMenu.setAwardValues(StringUtils.join(param.getAwardsIdList(), ","));
        bdMenu.setMenuValues(StringUtils.join(param.getMenuValueList(), ","));
        bdMenu.setAuthorityValues(StringUtils.join(param.getAuthorityIdList(), ","));
        baseMapper.updateById(bdMenu);

        // 删除权限配置
        UpdateWrapper<BdMenuAuthority> menuAuthorityWrapper = new UpdateWrapper<>();
        menuAuthorityWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        menuAuthorityWrapper.eq("PK_MENU", param.getMenuId());
        menuAuthorityWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        menuAuthorityWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        menuAuthorityWrapper.set(SystemFieldConstants.MODIFIED_TIME, nowDate);
        bdMenuAuthorityMapper.update(null, menuAuthorityWrapper);

        List<BdMenuAuthority> menuAuthoritieList = new ArrayList<>();
        // 等级处理
        for (Integer integer : param.getGradeIdList()) {
            BdMenuAuthority bdMenuAuthority = new BdMenuAuthority();
            bdMenuAuthority.setPkMenu(bdMenu.getPkId());
            bdMenuAuthority.setAuthorityType(EPermissionType.GRADE.getValue());
            bdMenuAuthority.setAuthorityValue(integer);
            bdMenuAuthority.setPkCountry(pkCountry);
            bdMenuAuthority.setPkCreator(userId);
            bdMenuAuthority.setCreationTime(nowDate);
            menuAuthoritieList.add(bdMenuAuthority);
        }
        // 奖衔处理
        for (Integer integer : param.getAwardsIdList()) {
            BdMenuAuthority bdMenuAuthority = new BdMenuAuthority();
            bdMenuAuthority.setPkMenu(bdMenu.getPkId());
            bdMenuAuthority.setAuthorityType(EPermissionType.AWARDS.getValue());
            bdMenuAuthority.setAuthorityValue(integer);
            bdMenuAuthority.setPkCountry(pkCountry);
            bdMenuAuthority.setPkCreator(userId);
            bdMenuAuthority.setCreationTime(nowDate);
            menuAuthoritieList.add(bdMenuAuthority);
        }
        // 权限处理
        for (Integer integer : param.getAuthorityIdList()) {
            BdMenuAuthority bdMenuAuthority = new BdMenuAuthority();
            bdMenuAuthority.setPkMenu(bdMenu.getPkId());
            bdMenuAuthority.setAuthorityType(EPermissionType.AUTHORITY.getValue());
            bdMenuAuthority.setAuthorityValue(integer);
            bdMenuAuthority.setPkCountry(pkCountry);
            bdMenuAuthority.setPkCreator(userId);
            bdMenuAuthority.setCreationTime(nowDate);
            menuAuthoritieList.add(bdMenuAuthority);
        }
        bdMenuAuthorityMapper.insertBatch(menuAuthoritieList);

        // 删除菜单明细配置
        UpdateWrapper<BdMenuDetail> menuDetailWrapper = new UpdateWrapper<>();
        menuDetailWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        menuDetailWrapper.eq("PK_MENU", param.getMenuId());
        menuDetailWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        menuDetailWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        menuDetailWrapper.set(SystemFieldConstants.MODIFIED_TIME, nowDate);
        bdMenuDetailMapper.update(null, menuDetailWrapper);

        List<BdMenuDetail> menuDetailList = new ArrayList<>();
        // 菜单明细项维护
        for (Integer integer : param.getMenuValueList()) {
            BdMenuDetail bdMenuDetail = new BdMenuDetail();
            bdMenuDetail.setPkMenu(bdMenu.getPkId());
            bdMenuDetail.setDetailValue(integer);
            bdMenuDetail.setPkCountry(pkCountry);
            bdMenuDetail.setPkCreator(userId);
            bdMenuDetail.setCreationTime(nowDate);
            menuDetailList.add(bdMenuDetail);
        }
        bdMenuDetailMapper.insertBatch(menuDetailList);

        return true;
    }

}
