package com.app.admin.framework.system.module.service.impl;

import com.app.admin.framework.system.module.convert.SysModuleConvert;
import com.app.admin.framework.system.module.dao.SysModuleDao;
import com.app.admin.framework.system.module.enums.SysModuleBtnEnum;
import com.app.admin.framework.system.module.model.SysModule;
import com.app.admin.framework.system.module.service.SysModuleService;
import com.app.framework.core.exception.ValidUtils;
import com.app.framework.core.support.enums.common.StateEnum;
import com.app.framework.core.support.service.impl.CrudBaseServiceSupport;
import com.app.framework.core.support.view.grid.Paging;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 资源管理
 */
@Service
public class SysModuleServiceImpl extends CrudBaseServiceSupport<SysModuleDao, SysModule> implements SysModuleService {

    private boolean checkMark(SysModule sysModule) {
        LambdaQueryWrapper<SysModule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysModule::getMark, sysModule.getMark());
        int count = this.count(wrapper);
        return count > 0;
    }

    /**
     * 列表权限按钮
     *
     * @param parentId
     * @return
     */
    private List<SysModule> getFuncBtn(Long parentId) {
        LambdaQueryWrapper<SysModule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysModule::getParentId, parentId);
        wrapper.eq(SysModule::getMtype, 2);
        return this.list(wrapper);
    }

    private SysModule saveEntity(SysModule sysModule) {
        if (sysModule.getMtype() == null) {
            sysModule.setMtype(1);
        }
        if (sysModule.getState() == null) {
            sysModule.setState(StateEnum.NORMAL.getValue());
        }
        if (sysModule.getOid() == null) {
            this.save(sysModule);
        } else {
            this.updateById(sysModule);
        }
        return sysModule;
    }

    /**
     * 查询所有记录,支持分页
     *
     * @param view   查询条件
     * @param paging 分页条件(paging为null或isPage为false时不分页)
     * @return List
     */
    public List<SysModule> getPageList(SysModule view, Paging paging) {
        LambdaQueryWrapper<SysModule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysModule::getOrganizationId, view.getOrganizationId());
        if (view.getMtype() != null) {
            wrapper.eq(SysModule::getMtype, view.getMtype());
        }
        if (view.getState() != null) {
            wrapper.eq(SysModule::getState, view.getState());
        }
        if (view.getParentId() != null) {
            wrapper.eq(SysModule::getParentId, view.getParentId());
        }
        if (StringUtils.isNotBlank(view.getName())) {
            wrapper.like(SysModule::getName, view.getName());
        }
        if (view.getVisiturlType() != null && view.getVisiturlType() == 0) {
            wrapper.in(SysModule::getVisiturlType, new Integer[]{1, 2});
        }
        wrapper.orderByAsc(SysModule::getMtype);
        wrapper.orderByAsc(SysModule::getMorder);
        return this.pageList(wrapper, paging);
    }

    public SysModule saveModule(SysModule view, String[] mtypeBtn) {
        SysModule sysModule = null;
        if (view.getOid() == null) {
            ValidUtils.valid(this.checkMark(view), "代码重复");
            sysModule = new SysModule();
            BeanUtils.copyProperties(view, sysModule);
        } else {
            sysModule = this.getById(view.getOid());
            sysModule.setParentId(view.getParentId());
            sysModule.setName(view.getName());
            sysModule.setMark(view.getMark());
            sysModule.setEnglishName(view.getEnglishName());
            sysModule.setVisiturlType(view.getVisiturlType());
            sysModule.setVisiturl(view.getVisiturl());
            sysModule.setParams(view.getParams());
            sysModule.setMtype(view.getMtype());
            sysModule.setState(view.getState());
            sysModule.setIconcls(view.getIconcls());
            sysModule.setMorder(view.getMorder());
            sysModule.setRemark(view.getRemark());
        }
        sysModule = this.saveEntity(sysModule);

        //同时新增权限按钮
        this.saveModuleBtn(sysModule, mtypeBtn);

        return sysModule;
    }

    public void saveModuleBtn(SysModule sysModule, String[] mtypeBtn) {
        if (!sysModule.getMtype().equals(1)) {
            return;
        }
        if (mtypeBtn == null || mtypeBtn.length < 1) {
            return;
        }
        //已入库的权限按钮
        List<SysModule> funcBtnList = this.getFuncBtn(sysModule.getOid());
        Map<String, SysModule> funcBtnMap = SysModuleConvert.toFuncBtnMap(funcBtnList);

        //需创建的权限按钮
        for (int i = 0; i < mtypeBtn.length; i++) {
            SysModuleBtnEnum btnEnum = SysModuleBtnEnum.valueOf(mtypeBtn[i]);
            SysModule module = SysModuleConvert.buildBtn(btnEnum, sysModule);

            SysModule exist = funcBtnMap.get(module.getMark());
            if (exist != null) {//已创建
                module.setOid(exist.getOid());
                module.setState(StateEnum.NORMAL.getValue());
            }
            this.saveEntity(module);
        }
    }

}