package com.css.simulation.resource.server.app.service;

import api.common.pojo.constants.DictConstants;
import api.common.pojo.constants.LogConstants;
import api.common.pojo.param.system.ParameterParam;
import api.common.pojo.po.system.ParameterPO;
import api.common.pojo.vo.system.ParameterVO;
import api.common.util.ObjectUtil;
import api.common.util.StringUtil;
import com.css.simulation.resource.server.infra.util.AuthUtil;
import com.css.simulation.resource.server.infra.util.PageUtil;
import com.css.simulation.resource.server.infra.util.PoUtil;
import com.css.simulation.resource.server.infra.db.mysql.mapper.ParameterMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service
public class ParameterService {

    @Autowired
    ParameterMapper parameterMapper;

    @Autowired
    LogService logService;


    public PageInfo<ParameterVO> getParameterList(ParameterParam pageParam) {
        PageUtil.setPageInfo(pageParam);
        List<ParameterVO> list = parameterMapper.getParameterList(pageParam);
        return new PageInfo<>(list);
    }

    public ParameterPO saveParameter(ParameterPO paramPO) {
        String id = paramPO.getId();
        if (ObjectUtil.isNotNull(id)) {
            // 更新，将原纪录is_deleted置为1
            ParameterPO deleteParamPo = new ParameterPO();
            deleteParamPo.setId(id);
            PoUtil.initDelPo(deleteParamPo);
            parameterMapper.deleteById(deleteParamPo);
        }
        //新增一条纪录
        paramPO.setId(StringUtil.getRandomUUID());
        PoUtil.initAddPo(paramPO);
        parameterMapper.insert(paramPO);
        logService.logParameter(LogConstants.SYS_LOG_PARAM_INSERT, paramPO);
        return paramPO;
    }

    public PageInfo<ParameterVO> getParamHistory(ParameterParam pageParam) {
        PageUtil.setPageInfo(pageParam);
        List<ParameterVO> list = parameterMapper.getParamHistory(pageParam);
        return new PageInfo<>(list);
    }

    public HashMap<String, Integer> getSceneParam() {
        String roleCode = AuthUtil.getCurrentUserRoleCode();
        String userId = AuthUtil.getCurrentUserId();
        HashMap<String, Integer> map = new HashMap<>();
        if (DictConstants.ROLE_CODE_ADMIN.equals(roleCode) || DictConstants.ROLE_CODE_SYSADMIN.equals(roleCode)) { //管理员账户
            return map;
        } else if (DictConstants.ROLE_CODE_SUBUESR.equals(roleCode)) {
            //查询父账户
            userId = AuthUtil.getCreateUserId();
        }

        return parameterMapper.getSceneParam(userId);
    }

    public ParameterVO getParameterByUserId() {
        String roleCode = AuthUtil.getCurrentUserRoleCode();
        if (DictConstants.ROLE_CODE_ADMIN.equals(roleCode) || DictConstants.ROLE_CODE_SYSADMIN.equals(roleCode)) {
            //1 管理员账户没有 可创建子账户数量、可创建测试包数量、测试包最大场景数量。
            return null;
        } else if (DictConstants.ROLE_CODE_UESR.equals(roleCode)) {
            //2 普通账户
            return parameterMapper.getParameterByUserId(AuthUtil.getCurrentUserId());
        } else {
            ParameterVO parentUser = parameterMapper.getParameterByUserId(AuthUtil.getCreateUserId());
            //3 子账户
            return ParameterVO.builder()
                    .numCreateUser(0)
                    .numCreateScenePackage(parentUser.getNumCreateScenePackage())
                    .numScenePerPackage(parentUser.getNumScenePerPackage())
                    .build();
        }
    }
}
