package com.engine.organization.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.hrm.entity.RuleCodeType;
import com.engine.organization.entity.codesetting.bo.CodeSettingBO;
import com.engine.organization.entity.codesetting.param.CodeSaveParam;
import com.engine.organization.entity.codesetting.po.CodeRuleDetailPO;
import com.engine.organization.entity.codesetting.po.CodeRulePO;
import com.engine.organization.mapper.codesetting.CodeRuleDetailMapper;
import com.engine.organization.mapper.codesetting.CodeRuleMapper;
import com.engine.organization.service.CodeSettingService;
import com.engine.organization.util.OrganizationAssert;
import com.engine.organization.util.db.MapperProxyFactory;
import com.engine.organization.util.response.ReturnResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

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

/**
 * @Author weaver_cl
 * @description:
 * @Date 2022/5/30
 * @Version V1.0
 **/
public class CodeSettingServiceImpl extends Service implements CodeSettingService {

    public CodeSettingService getCodeSettingService(User user) {
        return ServiceUtil.getService(CodeSettingServiceImpl.class, user);
    }

    @Override
    public ReturnResult getHasRight(String serialtype) {
        boolean hasRight = false;
        Map<String, Object> retmap = new HashMap<>();
        switch (RuleCodeType.getByValue(serialtype)) {
            case SUBCOMPANY:
                hasRight = HrmUserVarify.checkUserRight("HrmCodeRuleSubcompany:All", user);
                break;
            case DEPARTMENT:
                hasRight = HrmUserVarify.checkUserRight("HrmCodeRuleDepartment:All", user);
                break;
            case JOBTITLES:
                hasRight = HrmUserVarify.checkUserRight("HrmCodeRuleJobtitles:All", user);
                break;
            case USER:
                hasRight = HrmUserVarify.checkUserRight("HrmCodeRuleUser:All", user);
                break;
        }
        retmap.put("hasRight", hasRight);
        return ReturnResult.successed(retmap);
    }

    @Override
    public String saveOrUpdateCodeSetting(CodeSaveParam params) {
        OrganizationAssert.notNull(params, "参数不能为空");
        Long codeRuleId = getCodeSettingService(user).getCodeRuleId(params.getSerialType());
        CodeRulePO codeRulePO = CodeSettingBO.buildCodeRule(params, (long) user.getUID(), codeRuleId);
        //1.新增或更新主表
        if (codeRuleId != null) {
            MapperProxyFactory.getProxy(CodeRuleMapper.class).updateCodeRule(codeRulePO);
        } else {
            MapperProxyFactory.getProxy(CodeRuleMapper.class).insertCodeRule(codeRulePO);
        }

        //2.插入明细表
        codeRuleId = getCodeSettingService(user).getCodeRuleId(params.getSerialType());
        MapperProxyFactory.getProxy(CodeRuleDetailMapper.class).delete(codeRuleId);
        List<CodeRuleDetailPO> codeRuleDetailPOs = CodeSettingBO.bulidCodeDetailList(params.getCodeSaveDetailParams(), (long) user.getUID(), codeRuleId);
        if (CollectionUtils.isEmpty(codeRuleDetailPOs)) {
            MapperProxyFactory.getProxy(CodeRuleDetailMapper.class).batchInsert(codeRuleDetailPOs);
        }
        Map<String, Object> apidatas = new HashMap<>();
        apidatas.put("coderuleid", codeRuleId);
        return JSONObject.toJSONString(apidatas);
    }

    @Override
    public Long getCodeRuleId(String serialType) {
        return MapperProxyFactory.getProxy(CodeRuleMapper.class).getCodeRuleId(serialType);
    }

    @Override
    public String getCodeSetting(String serialType) {
        Map<String, Object> datas = new HashMap<>();
        CodeRulePO codeRulePO = MapperProxyFactory.getProxy(CodeRuleMapper.class).getCodeRuleByType(serialType);
        if (null != codeRulePO) {
            datas.put("serialtype", serialType);
            datas.put("serialenable", Util.null2String(codeRulePO.getSerialEnable(), "0"));
            String oneselftype = Util.null2String(codeRulePO.getOneselfType(), "");
            if (StringUtils.isNotEmpty(oneselftype)) {
                String[] oneselftypes = oneselftype.split(",");
                // 日期流水
                Map<String, String> dateSerialMap = new HashMap<>();
                dateSerialMap.put("enable", "0");
                // 机构流水
                Map<String, String> deptSerialMap = new HashMap<>();
                deptSerialMap.put("enable", "0");
                // 岗位
                Map<String, String> jobtitlesSerialMap = new HashMap<>();
                jobtitlesSerialMap.put("enable", "0");
                for (String type : oneselftypes) {
                    switch (RuleCodeType.getByValue(type)) {
                        case YEAR:
                        case MONTH:
                        case DAY:
                            dateSerialMap.put("enable", "1");
                            dateSerialMap.put("key", type);
                            break;
                        case SUBCOMPANY:
                        case DEPARTMENT:
                            deptSerialMap.put("enable", "1");
                            deptSerialMap.put("key", type);
                            break;
                        case JOBTITLES:
                            jobtitlesSerialMap.put("enable", "1");
                            jobtitlesSerialMap.put("key", type);
                            break;
                    }
                }
                datas.put("dateSerial", dateSerialMap);
                datas.put("deptSerial", deptSerialMap);
                datas.put("jobtitlesSerial", jobtitlesSerialMap);
            }

        }
        return null;
    }
}
