package com.engine.salary.service.impl;

import com.api.formmode.mybatis.util.SqlProxyHandle;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.SalarySobExtRangePO;
import com.engine.salary.entity.salarysob.param.SalarySobRangeExtSaveParam;
import com.engine.salary.entity.salarysob.param.SalarySobRangeQueryParam;
import com.engine.salary.entity.salarysob.po.SalarySobPO;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.mapper.salarysob.SalarySobExtRangeMapper;
import com.engine.salary.mapper.sys.SalarySysConfMapper;
import com.engine.salary.service.SalaryEmployeeService;
import com.engine.salary.service.SalarySobExtRangeService;
import com.engine.salary.service.SalarySobService;
import com.engine.salary.service.TaxAgentService;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
import com.engine.salary.util.valid.ValidUtil;
import com.engine.salary.util.db.IdGenerator;
import org.apache.commons.collections4.CollectionUtils;
import weaver.hrm.User;

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

/**
 * 薪资账套人员范围
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalarySobExtRangeServiceImpl extends Service implements SalarySobExtRangeService {



    private SalarySobService getSalarySobService(User user) {
        return  ServiceUtil.getService(SalarySobServiceImpl.class, user);
    }

    private TaxAgentService getTaxAgentService(User user) {
        return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
    }

    private SalaryEmployeeService getSalaryEmployeeService(User user) {
        return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
    }

    private SalarySysConfMapper getSalarySysConfMapper() {
        return SqlProxyHandle.getProxy(SalarySysConfMapper.class);
    }

    private SalarySobExtRangeMapper getSalarySobExtRangeMapper() {
        return SqlProxyHandle.getProxy(SalarySobExtRangeMapper.class);
    }


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

    @Override
    public List<SalarySobExtRangePO> listBySalarySobId(Long salarySobId) {
        if (salarySobId ==null) {
            return Collections.emptyList();
        }
        return getSalarySobExtRangeMapper().listSome(SalarySobExtRangePO.builder().salarySobId(salarySobId).build());
    }


    @Override
    public void saveExtRange(SalarySobRangeExtSaveParam saveParam) {
        ValidUtil.doValidator(saveParam);

        // 查询薪资账套
        SalarySobPO salarySobPO = getSalarySobService(user).getById(saveParam.getSalarySobId());
        if (Objects.isNull(salarySobPO)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98379, "参数错误，薪资账套不存在或者已被删除"));
        }
        // 查询已有的人员范围
        List<SalarySobExtRangePO> salarySobRangePOS = getSalarySobExtRangeMapper().listSome(SalarySobExtRangePO.builder().salarySobId(saveParam.getSalarySobId()).build());
        // 处理一下本次的保存参数（如果原来添加过对应的人员（/部门/岗位），那么本次不需要新增，只需要更新）
        List<Long> oldTargetIds = SalaryEntityUtil.properties(salarySobRangePOS, SalarySobExtRangePO::getTargetId, Collectors.toList());

        List<Long> targetIds = saveParam.getTargetIds();
        Date date = new Date();
        if (CollectionUtils.isNotEmpty(targetIds)) {
            targetIds.stream().filter(targetId -> !oldTargetIds.contains(targetId)).forEach(targetId -> {
                SalarySobExtRangePO po = SalarySobExtRangePO.builder()
                        .id(IdGenerator.generate())
                        .targetType(1)
                        .salarySobId(saveParam.getSalarySobId())
                        .targetId(targetId)
                        .createTime(date)
                        .updateTime(date)
                        .creator((long) user.getUID())
                        .deleteType(0)
                        .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                        .build();
                getSalarySobExtRangeMapper().insertIgnoreNull(po);
            });
        }
    }

    @Override
    public PageInfo<SalarySobExtRangePO> listPage4Ext(SalarySobRangeQueryParam param) {
        List<SalarySobExtRangePO> salarySobRangePOS = getSalarySobExtRangeMapper().listPage4Ext(SalarySobExtRangePO.builder().salarySobId(param.getSalarySobId()).build());
        return SalaryPageUtil.buildPage(param.getCurrent(), param.getPageSize(), salarySobRangePOS, SalarySobExtRangePO.class);
    }


    @Override
    public void deleteSalarySobExtRange(Collection<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return;
        }
        getSalarySobExtRangeMapper().deleteByIds(ids);
    }

}
