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.EmpCostType;
import com.kcim.dao.repository.EmpCostTypeRepository;
import com.kcim.service.CenterService;
import com.kcim.service.EmpCostTypeService;
import com.kcim.vo.CodeAndNameVO;
import com.kcim.vo.DictDataVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

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

/**
 * @program: CostAccount
 * @description: 人事分类接口实现类
 * @author: Wang.YS
 * @create: 2023-11-02 15:50
 **/
@Service("EmpCostTypeService")
@Slf4j
@AllArgsConstructor
public class EmpCostTypeServiceImpl implements EmpCostTypeService {
    EmpCostTypeRepository repository;

    CenterService centerService;


    /**
     * 获取人事分类列表
     * @param empTypeName 人事分类名称
     * @return 列表
     */
    @Override
    public Object getEmpCostType(String empTypeName) {
        return repository.getList(empTypeName);
    }

    /**
     * 添加人事分类
     * @param request 入参
     */
    @Override
    public void addEmpCostType(EmpCostType request) {
        request.setHospId(UserContext.getHospId());
        request.setCreateUser(String.valueOf(UserContext.getCurrentUser().getId()));
        request.setCreateTime(new Date());
        request.setCostTypeCode(repository.getCode());
        repository.save(request);
    }

    /**
     * 删除人事分类
     * @param id 主键
     */
    @Override
    public void deleteEmpCostType(Integer id) {
        repository.deleteEmpCostType(id);
    }

    /**
     * 编辑人事分类
     * @param request 入参
     */
    @Override
    public void editEmpCostType(EmpCostType request) {
        Integer id = request.getId();
        if(Objects.isNull(id)){
            log.error("编辑时主键必传");
            throw new CostException("编辑时主键必传");
        }
        EmpCostType costType = repository.getById(id);
        BeanUtil.convertObj(request,costType);
        costType.setUpdateTime(new Date());
        costType.setUpdateUser(String.valueOf(UserContext.getCurrentUser().getId()));
        repository.updateById(costType);
    }

    /**
     * 获取报表计算类别
     * @return 计算类别
     */
    @Override
    public Object getEmpCostTypeDict() {
        DictDataVo dict = centerService.getDict(EMP_CHILD_TYPE);
        return dict.getDataVoList();
    }

    @Override
    public List<CodeAndNameVO> getItemDict() {
        List<EmpCostType> list = repository.getList();

        if(!CollectionUtils.isEmpty(list)){
        List<CodeAndNameVO> returnList = new ArrayList<>();
            for (EmpCostType type : list) {
                CodeAndNameVO vo = new CodeAndNameVO();
                vo.setCode(type.getCostTypeCode());
                vo.setName(type.getCostTypeName());
                returnList.add(vo);
            }
            //添加全部
            CodeAndNameVO vo = new CodeAndNameVO();
            vo.setCode(NumberConstant.ZERO_S);
            vo.setName("全部");
            returnList.add(vo);
            return returnList;
        }
        return new ArrayList<>();
    }
}
