package com.kcim.service.impl;

import com.kcim.common.constants.NumberConstant;
import com.kcim.common.exception.CostException;
import com.kcim.common.util.BeanUtil;
import com.kcim.common.util.UserContext;
import com.kcim.dao.model.ShareParamTypeMap;
import com.kcim.dao.repository.ShareParamTypeMapRepository;
import com.kcim.service.*;
import com.kcim.vo.CodeAndNameVO;
import com.kcim.vo.DictDataVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.matcher.FilterableList;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

import static com.kcim.common.constants.Constant.ITEM_ALLOCATE_PARAM_SOURCE_TYPE;

/**
 * @program: CostAccount
 * @description: 项目分摊参数实现类
 * @author: Wang.YS
 * @create: 2024-01-15 10:10
 **/
@Service("ShareParamTypeMapService")
@Slf4j
@AllArgsConstructor
public class ShareParamTypeMapServiceImpl implements ShareParamTypeMapService {
    /**
     * 项目分摊参数数据库交互
     */
    ShareParamTypeMapRepository repository;
    /**
     * 人事分类字典
     */
    EmpCostTypeService empCostTypeService;

    EquipmentService equipmentService;

    SpaceService spaceService;

    CenterService centerService;

    /**
     * @param filter
     * @return
     */
    @Override
    public Object getList(String filter) {
        List<ShareParamTypeMap> list = repository.getList(filter);
        if(!CollectionUtils.isEmpty(list)){
            //获取 项目分摊参数来源类型字典
            DictDataVo dict = centerService.getDict(ITEM_ALLOCATE_PARAM_SOURCE_TYPE);
            List<DictDataVo> dataVoList = dict.getDataVoList();
            Map<String,String> itemTypeMap = dataVoList.stream().collect(Collectors.toMap(DictDataVo::getCode, DictDataVo::getName, (a, b) -> b));
            for (ShareParamTypeMap paramTypeMap : list) {
                paramTypeMap.setSourceTypeName(itemTypeMap.get(paramTypeMap.getSourceType()));
            }
            return list;
        }
        return new ArrayList<>();
    }

    /**
     * @param request
     */
    @Override
    public void add(ShareParamTypeMap request) {
//        shareParamCode
        ShareParamTypeMap byShareParamCode = repository.getByShareParamCode(request.getShareParamCode());
        if(Objects.nonNull(byShareParamCode)){
            throw new CostException("已存在相同分摊参数代码，请修改后再保存");
        }
        request.setHospId(UserContext.getHospId());
        request.setCreateTime(new Date());
        request.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
        repository.save(request);

    }

    /**
     * @param request
     */
    @Override
    public void update(ShareParamTypeMap request) {
        if(request.getId()==null){
            throw new CostException("编辑时主键必填");

        }
        ShareParamTypeMap byShareParamCode = repository.getByShareParamCode(request.getShareParamCode());

        ShareParamTypeMap paramTypeMap = repository.getById(request.getId());
        if(Objects.isNull(byShareParamCode)|| byShareParamCode.getId().equals(request.getId())){
            BeanUtil.convertObj(request, paramTypeMap);
            paramTypeMap.setUpdateUser(String.valueOf(UserContext.getCurrentUser().getId()));
            paramTypeMap.setUpdateTime(new Date());
            repository.updateById(paramTypeMap);
        }else {
            throw new CostException("已存在相同分摊参数代码，请修改后再保存");

        }

    }

    /**
     * @param id
     */
    @Override
    public void delete(Integer id) {
        repository.removeByShareParamId(id);
    }

    /**
     * @param type
     * @return
     */
    @Override
    public Object getSourceItem(Integer type) {
        // type 1 参数人员 2 使用设备 3 使用空间 4 项目收入
        List<CodeAndNameVO> list = new ArrayList<>();
        if(type.equals(NumberConstant.ONE)){
            list =  empCostTypeService.getItemDict();
        } else if (type.equals(NumberConstant.TWO)) {
            list = equipmentService.getItemDict();
        } else if (type.equals(NumberConstant.THREE)) {
            list = spaceService.getItemDict();
        } else if (type.equals(NumberConstant.FOUR)) {
            CodeAndNameVO vo = new CodeAndNameVO();
            vo.setCode("itemIncome");
            vo.setName("项目收入");
            list.add(vo);
        } else if (type.equals(NumberConstant.FIVE)) {
            CodeAndNameVO vo = new CodeAndNameVO();
            vo.setCode("itemNum");
            vo.setName("项目数量");
            list.add(vo);
        }
        return list;
    }
}
