package com.zhgd.xmgl.modules.basicdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhgd.xmgl.modules.basicdata.entity.*;
import com.zhgd.xmgl.modules.basicdata.mapper.BaseActionMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.BaseMenuMapper;
import com.zhgd.xmgl.modules.basicdata.mapper.BaseModuleMapper;
import com.zhgd.xmgl.modules.basicdata.service.*;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import com.zhgd.xmgl.util.ParamEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 系统资源-模块表
 * @author： pds
 * @date：   2023-02-09
 * @version： V1.0
 */
@Service
public class BaseModuleServiceImpl extends ServiceImpl<BaseModuleMapper, BaseModule> implements IBaseModuleService {

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IModuleTemplateService moduleTemplateService;

    @Autowired
    private BaseMenuMapper baseMenuMapper;

    @Autowired
    private BaseActionMapper baseActionMapper;

    @Autowired
    private ISystemDictDataService systemDictDataService;

    @Override
    public Page<BaseModule> pageList(Page page, Wrapper<BaseModule> wrapper) {
        Page<BaseModule> pageList = baseMapper.selectPage(page, wrapper);
        List<SystemDictData> moduleType = systemDictDataService.getByType(ParamEnum.SysDictType.MODULE_TYPE.getValue());
        List<SystemDictData> moduleStyleType = systemDictDataService.getByType(ParamEnum.SysDictType.MODULE_STYLE_TYPE.getValue());
        for (BaseModule baseModule : pageList.getRecords()) {
            SystemDictData typeData = moduleType.stream().filter(m -> m.getDictLabel() == baseModule.getModuleType()).findFirst().get();
            baseModule.setModuleTypeName(typeData == null ? "" : typeData.getDictValue());
            SystemDictData styleData = moduleStyleType.stream().filter(m -> m.getDictLabel() == baseModule.getStyleType()).findFirst().get();
            baseModule.setStyleTypeName(styleData == null ? "" : styleData.getDictValue());
        }
        return pageList;
    }

    @Override
    public List<BaseModule> queryBySelf() {
        SecurityUser user = SecurityUtil.getUser();
        // 查询用户所属政务
        String governmentSn = null;
        if (user.getAccountType() == 2) {
            governmentSn = user.getSn();
        } else if (user.getAccountType() == 3){
            Enterprise enterprise = enterpriseService.getOne(Wrappers.<Enterprise>lambdaQuery().eq(Enterprise::getGovernmentSn, user.getSn()));
            governmentSn = enterprise.getGovernmentSn();
        } else if (user.getAccountType() == 4){
            Project project = projectService.getOne(Wrappers.<Project>lambdaQuery().eq(Project::getGovernmentSn, user.getSn()));
            governmentSn = project.getGovernmentSn();
        }
        // TODO 一步到位
        List<Long> ids = moduleTemplateService.list(Wrappers.<ModuleTemplate>lambdaQuery()
                .eq(ModuleTemplate::getModuleType, user.getAccountType())
                .eq(ModuleTemplate::getGovernmentSn, governmentSn)).stream().map(b -> b.getModuleId()).collect(Collectors.toList());
        return this.list(Wrappers.<BaseModule>lambdaQuery()
                .in(BaseModule::getModuleId, ids).orderByDesc(BaseModule::getIsImportantModule).orderByAsc(BaseModule::getLabelSortNum, BaseModule::getPriority));
    }

    @Override
    public boolean removeInfo(String id) {
        List<String> actionIds = baseActionMapper.getByModuleId(id);
        baseActionMapper.deleteBatchIds(actionIds);
        baseMenuMapper.delete(Wrappers.<BaseMenu>lambdaQuery().eq(BaseMenu::getModuleId, id));
        return this.removeById(id);
    }
}
