package com.xhpc.activity.service.impl;

import com.xhpc.common.core.exception.CustomException;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.activity.domain.XhpcWorkTypeDictDomain;
import com.xhpc.activity.mapper.XhpcWorkTypeDictMapper;
import com.xhpc.activity.service.WorkTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class WorkTypeServiceImpl implements WorkTypeService {

    @Resource
    XhpcWorkTypeDictMapper typeDictMapper;


    @Override
    public List<XhpcWorkTypeDictDomain> getPage(Map<String, Object> params){
        return typeDictMapper.selectListByParams(params);
    }

    @Override
    public List<Map<String, Object>> getList(Map<String, Object> params){
        return typeDictMapper.selectMapListByParams(params);
    }


    @Override
    public List<Map<String, Object>> getTree(Map<String, Object> params){
        List<Map<String, Object>> typeList = typeDictMapper.selectMapListByParentParams(params);
        for (Map<String, Object> map : typeList){
            params.put("parentTypeId", map.get("id"));
            List<Map<String, Object>> childList = typeDictMapper.selectMapListByParentParams(params);
            for (Map<String, Object> childMap: childList){
                params.put("parentTypeId", childMap.get("id"));
                List<Map<String, Object>> dataList = typeDictMapper.selectMapListByParentParams(params);
                if(dataList.size() > 0){
                    childMap.put("children", dataList);
                }
            }

            if (childList.size() > 0){
                map.put("children", childList);
            }
        }

        return typeList;
    }


    @Override
    public XhpcWorkTypeDictDomain getInfoByPk(Integer typeId){
        return typeDictMapper.selectByPrimaryKey(typeId);
    }


    @Override
    public Boolean insertTypeInfo(XhpcWorkTypeDictDomain domain){
        if(StringUtils.isEmpty(domain.getName())){
            throw new CustomException("名称不能为空");
        }
        XhpcWorkTypeDictDomain exitsDomain = typeDictMapper.selectByName(domain.getName(), domain.getParentTypeId());
        if(exitsDomain != null) {
            throw new CustomException("名称不能重复");
        }
        if(domain.getAutoSend() == 1 && (domain.getUserId() == null || domain.getUserId() < 1)){
            throw new CustomException("请选择派发人");
        }
        if(domain.getAutoRun() == 1 && (domain.getMeasure() == null || domain.getMeasure() < 1)){
            throw new CustomException("请选择自动执行措施");
        }

        return typeDictMapper.insertSelective(domain) > 0;
    }


    @Override
    public Boolean updateTypeInfo(XhpcWorkTypeDictDomain domain){
        if(StringUtils.isEmpty(domain.getName())){
            throw new CustomException("名称不能为空");
        }

        if(domain.getWorkTypeId().equals(domain.getParentTypeId())){
            throw new CustomException("自身不能成为自身的子集");
        }

        XhpcWorkTypeDictDomain exitsDomain = typeDictMapper.selectByName(domain.getName(), domain.getParentTypeId());
        if(exitsDomain != null && !exitsDomain.getWorkTypeId().equals(domain.getWorkTypeId())) {
            throw new CustomException("名称不能重复");
        }
        if(domain.getAutoSend() == 1 && (domain.getUserId() == null || domain.getUserId() < 1)){
            throw new CustomException("请选择派发人");
        }
        if(domain.getAutoRun() == 1 && (domain.getMeasure() == null || domain.getMeasure() < 1)){
            throw new CustomException("请选择自动执行措施");
        }
        return typeDictMapper.updateByPrimaryKeySelective(domain) > 0;
    }


    @Override
    public Boolean deleteTypeInfo(Integer typeId){
        Map<String, Object> params = new HashMap<>();
        params.put("parentTypeId", typeId);

        List<Map<String, Object>> typeList = typeDictMapper.selectMapListByParentParams(params);
        if(typeList.size() > 0){
            throw new CustomException("存在类型子集，请先移除下级问题类型");
        }

        return typeDictMapper.deleteByPrimaryKeyLogic(typeId) > 0;
    }
}
