package com.engine.salary.wrapper;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.salaryitem.bo.SysSalaryItemBO;
import com.engine.salary.entity.salaryitem.dto.SysSalaryItemListDTO;
import com.engine.salary.entity.salaryitem.param.SysSalaryItemSearchParam;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salaryitem.po.SysSalaryItemPO;
import com.engine.salary.enums.SalarySystemTypeEnum;
import com.engine.salary.service.SalaryItemService;
import com.engine.salary.service.SysSalaryItemService;
import com.engine.salary.service.impl.SalaryItemServiceImpl;
import com.engine.salary.service.impl.SysSalaryItemServiceImpl;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
import weaver.hrm.User;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 系统薪资项目
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/

public class SysSalaryItemWrapper extends Service {


    private SalaryItemService getSalaryItemService(User user) {
        return (SalaryItemService) ServiceUtil.getService(SalaryItemServiceImpl.class, user);
    }

    private SysSalaryItemService getSysSalaryItemService(User user) {
        return (SysSalaryItemService) ServiceUtil.getService(SysSalaryItemServiceImpl.class, user);
    }


    /**
     * 系统薪资项目列表
     *
     * @param searchParam 高级搜索的查询条件
     * @return
     */
    public PageInfo<SysSalaryItemListDTO> listPage(SysSalaryItemSearchParam searchParam) {
        // 查询已经添加过的系统薪资项目（已经添加过的系统薪资项目不能二次添加）
        List<SalaryItemPO> salaryItemPOS = getSalaryItemService(user).listBySystemType(SalarySystemTypeEnum.SYSTEM);
        Set<Long> sysSalaryItemIds = SalaryEntityUtil.properties(salaryItemPOS, SalaryItemPO::getSysSalaryItemId);
        searchParam.setExcludeIds(sysSalaryItemIds);

        List<SysSalaryItemPO> sysSalaryItemPOS = getSysSalaryItemService(user).listLikeName(SysSalaryItemPO.builder().name(searchParam.getName()).build());
        List<SysSalaryItemPO> finalItems = sysSalaryItemPOS.stream().filter(item -> !sysSalaryItemIds.contains(item.getId())).collect(Collectors.toList());
        List<SysSalaryItemListDTO> sysSalaryItemListDTOS = SysSalaryItemBO.convert2ListDTO(finalItems);
        // 分页参数
        PageInfo<SysSalaryItemListDTO> pageInfo = SalaryPageUtil.buildPage(searchParam.getCurrent(), searchParam.getPageSize(), sysSalaryItemListDTOS, SysSalaryItemListDTO.class);
        return pageInfo;
    }

//    private String makeSqlWhere(SysSalaryItemSearchParam searchParam) {
//        DBType dbType = DBType.get(new RecordSet().getDBType());
//        String sqlWhere = " t.delete_type = 0 ";
//
//        String name = searchParam.getName();
//        if (StringUtils.isNotBlank(name)) {
//            sqlWhere += " AND t.name " + dbType.like(name);
//        }
//
//        String itemType = searchParam.getItemType();
//        if (StringUtils.isNotBlank(itemType)) {
//            sqlWhere += " AND t.item_type = " + itemType;
//        }
//
//        String category = searchParam.getCategory();
//        if (StringUtils.isNotBlank(category)) {
//            sqlWhere += " AND t.category = " + category;
//        }
//
//        Collection<Long> excludeIds = searchParam.getExcludeIds();
//        if (CollectionUtils.isNotEmpty(excludeIds)) {
//            String idsStr = excludeIds.stream().map(String::valueOf).collect(Collectors.joining(","));
//            sqlWhere += " AND t.id NOT IN (" + idsStr + ")";
//        }
//
//
//        return sqlWhere;
//    }

//    /**
//     * 获取高级搜索
//     *
//     * @return
//     */
//    public WeaSearchCondition getSearchCondition() {
//        WeaSearchCondition searchCondition = SalaryFormatUtil.<SysSalaryItemSearchConditionDTO>getInstance()
//                .buildCondition(SysSalaryItemSearchConditionDTO.class, new SysSalaryItemSearchConditionDTO(), "sysSalaryItemCondition");
//        // 其他条件不要
//        searchCondition.getGroups().remove(1);
//        return searchCondition;
//    }

    /**
     * 添加系统薪资项目到自定义薪资项目列表中
     *
     * @param ids 主键id
     */
    public void add2SalaryItem(Collection<Long> ids) {
        getSysSalaryItemService(user).add2SalaryItem(ids);
    }
}
