package com.engine.salary.service.impl;

import com.engine.core.impl.Service;
import com.engine.salary.cmd.TaxRate.*;
import com.engine.salary.entity.taxrate.SysTaxRateBase;
import com.engine.salary.entity.taxrate.TaxRateBase;
import com.engine.salary.mapper.SysTaxRateBaseMapper;
import com.engine.salary.mapper.TaxRateBaseMapper;
import com.engine.salary.service.TaxRateBaseService;
import com.engine.salary.util.db.MapperProxyFactory;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class TaxRateBaseServiceImpl extends Service implements TaxRateBaseService {


    private SysTaxRateBaseMapper getSysTaxRateBaseMapper(){
        return MapperProxyFactory.getProxy(SysTaxRateBaseMapper.class);
    }
    

    private TaxRateBaseMapper getTaxRateBaseMapper(){
        return MapperProxyFactory.getProxy(TaxRateBaseMapper.class);
    }

    @Override
    public Map<String, Object> listPage(Map<String, Object> params) {
        return commandExecutor.execute(new TaxRateListCmd(params, user));
    }


    @Override
    public Map<String, Object> save(Map<String, Object> params) {
        return commandExecutor.execute(new TaxRateSaveCmd(params, user));
    }

    @Override
    public Map<String, Object> update(Map<String, Object> params) {
        return commandExecutor.execute(new TaxRateUpdateCmd(params, user));
    }

    @Override
    public Map<String, Object> getForm(Map<String, Object> params) {
        return commandExecutor.execute(new TaxRateGetFormCmd(params, user));
    }


    @Override
    public Map<String, Object> delete(Map<String, Object> params) {
        return commandExecutor.execute(new TaxRateDeleteCmd(params, user));
    }

    @Override
    public List<TaxRateBase> list() {
        List<TaxRateBase> resultList = Lists.newArrayList();
        // 查询系统默认的税率表
        List<SysTaxRateBase> sysTaxRateBasePOS = getSysTaxRateBaseMapper().listAll();

        List<TaxRateBase> taxRateBasePOS4Sys = sysTaxRateBasePOS.stream()
                .map(sysTaxRateBasePO -> {
                    TaxRateBase taxRateBasePO = new TaxRateBase();
                    BeanUtils.copyProperties(sysTaxRateBasePO, taxRateBasePO);
                    return taxRateBasePO;
                })
                .collect(Collectors.toList());
        resultList.addAll(taxRateBasePOS4Sys);
        // 查询自定义税率表
        List<TaxRateBase> taxRateBasePOS = getTaxRateBaseMapper().listAll();
        resultList.addAll(taxRateBasePOS);
        return resultList;
    }

}
