package com.engine.salary.service.impl;

import com.engine.core.impl.Service;
import com.engine.salary.biz.SalaryItemBiz;
import com.engine.salary.biz.SysSalaryItemBiz;
import com.engine.salary.config.SalaryElogConfig;
import com.engine.hrmelog.entity.dto.LoggerContext;
import com.engine.salary.entity.salaryitem.bo.SysSalaryItemBO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salaryitem.po.SysSalaryItemPO;
import com.engine.salary.enums.OperateTypeEnum;
import com.engine.salary.enums.SalarySystemTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.mapper.salaryitem.SysSalaryItemMapper;
import com.engine.salary.service.SysSalaryItemService;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.MapperProxyFactory;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 系统薪资项目
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SysSalaryItemServiceImpl extends Service implements SysSalaryItemService {
    private SysSalaryItemBiz sysSalaryItemMapper = new SysSalaryItemBiz();
    private SalaryItemBiz salaryItemService = new SalaryItemBiz();

    private SysSalaryItemMapper getSysSalaryItemMapper() {
        return MapperProxyFactory.getProxy(SysSalaryItemMapper.class);
    }
//    
//    private LoggerTemplate salaryItemLoggerTemplate;

    @Override
    public SysSalaryItemPO getById(Long id) {
        return sysSalaryItemMapper.getById(id);
    }

    @Override
    public List<SysSalaryItemPO> listByIds(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return sysSalaryItemMapper.listSome(SysSalaryItemPO.builder().ids(ids).build());
    }

    @Override
    public List<SysSalaryItemPO> listByName(String name) {
        if (StringUtils.isBlank(name)) {
            return Collections.emptyList();
        }
        return sysSalaryItemMapper.listSome(SysSalaryItemPO.builder().name(name).build());
    }

//    @Override
//    public Page<SysSalaryItemPO> listPageByParam(SysSalaryItemSearchParam searchParam) {
//        // 分页参数
//        Page<SysSalaryItemPO> page = SalaryPageUtil.buildPage(searchParam.getCurrent(), searchParam.getPageSize());
//        // 查询参数构建queryWrapper
//        Wrapper<SysSalaryItemPO> queryWrapper = SysSalaryItemBO.buildQueryWrapper(searchParam);
//        // 查询系统薪资项目
//        return sysSalaryItemMapper.selectPage(page, queryWrapper);
//    }

    @Override
    public void add2SalaryItem(Collection<Long> ids) {
        // 查询系统薪资项目
        List<SysSalaryItemPO> sysSalaryItemPOS = listByIds(ids);
        if (CollectionUtils.isEmpty(sysSalaryItemPOS)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98349, "参数错误，系统薪资项目不存在"));
        }
        // 查询已经添加过的系统薪资项目
        List<SalaryItemPO> salaryItemPOS = salaryItemService.listSome(SalaryItemPO.builder().systemType(SalarySystemTypeEnum.SYSTEM.getValue()).build());
        Set<Long> sysSalaryItemIds = SalaryEntityUtil.properties(salaryItemPOS, SalaryItemPO::getSysSalaryItemId);
        // 已经添加过的系统薪资项目不能重复添加
        boolean exist = sysSalaryItemPOS.stream().anyMatch(sysSalaryItemPO -> sysSalaryItemIds.contains(sysSalaryItemPO.getId()));
        if (exist) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98350, "已经添加过的系统薪资项目不能重复添加"));
        }
        // 保存
        List<SalaryItemPO> salaryItems = SysSalaryItemBO.convert2SalaryItemPO(sysSalaryItemPOS, (long) user.getUID());
        salaryItemService.batchSave(salaryItems);
        // 记录日志
        sysSalaryItemPOS.forEach(sysSalaryItemPO -> {
            LoggerContext<SalaryItemPO> loggerContext = new LoggerContext<>();
            loggerContext.setUser(user);
            loggerContext.setTargetId(String.valueOf(sysSalaryItemPO.getId()));
            loggerContext.setTargetName(sysSalaryItemPO.getName());
            loggerContext.setOperateType(OperateTypeEnum.ADD.getValue());
            loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "添加系统薪资项目"));
            loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "添加系统薪资项目") + ": " + sysSalaryItemPO.getName());
            loggerContext.setNewValues(sysSalaryItemPO);
            SalaryElogConfig.salaryItemLoggerTemplate.write(loggerContext);
        });
    }

    @Override
    public List<SysSalaryItemPO> listAll() {
        return getSysSalaryItemMapper().listAll();
    }

    @Override
    public List<SysSalaryItemPO> listSome(SysSalaryItemPO po) {
        return getSysSalaryItemMapper().listSome(po);
    }

    @Override
    public List<SysSalaryItemPO> listLikeName(SysSalaryItemPO po) {
        return getSysSalaryItemMapper().listLikeName(po);
    }
}
