package com.yiqi.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.yiqi.common.enums.SourceType;
import com.yiqi.common.enums.SysConfigKey;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.system.domain.ManageFactory;
import com.yiqi.system.domain.SysStore;
import com.yiqi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.system.mapper.SysSettlementRateMapper;
import com.yiqi.system.domain.SysSettlementRate;
import com.yiqi.system.service.ISysSettlementRateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 结算费率Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-17
 */
@Service
public class SysSettlementRateServiceImpl extends ServiceImpl<SysSettlementRateMapper,SysSettlementRate> implements ISysSettlementRateService {

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询结算费率
     *
     * @param id 结算费率主键
     * @return 结算费率
     */
    @Override
    public SysSettlementRate selectSysSettlementRateById(Long id) {
        return baseMapper.selectSysSettlementRateById(id);
    }

    /**
     * 查询结算费率列表
     *
     * @param sysSettlementRate 结算费率
     * @return 结算费率
     */
    @Override
    public List<SysSettlementRate> selectSysSettlementRateList(SysSettlementRate sysSettlementRate) {
        List<SysSettlementRate> list = new ArrayList<>();
        if (SourceType.FACTORY.getCode().equals(sysSettlementRate.getSourceType())){
            list = baseMapper.selectSysSettlementRateListByFactory(sysSettlementRate);
        }
        if (SourceType.STORE.getCode().equals(sysSettlementRate.getSourceType())){
            list = baseMapper.selectSysSettlementRateListByStore(sysSettlementRate);
        }
        return list;
    }

    /**
     * 新增结算费率
     *
     * @param sysSettlementRate 结算费率
     * @return 结果
     */
    @Override
    public int insertSysSettlementRate(SysSettlementRate sysSettlementRate) {
        sysSettlementRate.buildCreateData();
        return baseMapper.insertSysSettlementRate(sysSettlementRate);
    }

    /**
     * 修改结算费率
     *
     * @param sysSettlementRate 结算费率
     * @return 结果
     */
    @Override
    public int updateSysSettlementRate(SysSettlementRate sysSettlementRate) {
        sysSettlementRate.buildUpdateData();
        return baseMapper.updateSysSettlementRate(sysSettlementRate);
    }

    /**
     * 批量删除结算费率
     *
     * @param ids 需要删除的结算费率主键
     * @return 结果
     */
    @Override
    public int deleteSysSettlementRateByIds(Long[] ids) {
        return baseMapper.deleteSysSettlementRateByIds(ids);
    }

    /**
     * 删除结算费率信息
     *
     * @param id 结算费率主键
     * @return 结果
     */
    @Override
    public int deleteSysSettlementRateById(Long id) {
        return baseMapper.deleteSysSettlementRateById(id);
    }

    @Override
    public int insertFactoryRate(ManageFactory manageFactory) {
        SysSettlementRate sysSettlementRate = new SysSettlementRate();
        //材料费率
        sysSettlementRate.setMaterialRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementMaterialRate.getCode())));
        //管理费率
        sysSettlementRate.setManageRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementManageRate.getCode())));
        //押金费率
        sysSettlementRate.setDepositRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementDepositRate.getCode())));
        // 云洗费率
        sysSettlementRate.setFactoryRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementOnlineWashRate.getCode())));

//        //异店消费费率
//        sysSettlementRate.setOtherStoreRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementOtherStoreRate.getCode())));
//        //支付宝费率
//        sysSettlementRate.setAliPayRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementAliRate.getCode())));
//        //微信费率
//        sysSettlementRate.setWxPayRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementWXRate.getCode())));
        sysSettlementRate.setSourceType("01");
        sysSettlementRate.setOrgId(manageFactory.getId());
        return baseMapper.insertSysSettlementRate(sysSettlementRate);
    }

    @Override
    public int insertStoreRate(SysStore sysStore) {
        SysSettlementRate sysSettlementRate = new SysSettlementRate();
        //工厂结算费率
        sysSettlementRate.setFactoryRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementFactoryRate.getCode())));
        //材料费率
        sysSettlementRate.setMaterialRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementMaterialRate.getCode())));
        //管理费率
        sysSettlementRate.setManageRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementManageRate.getCode())));
        //押金费率
        sysSettlementRate.setDepositRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementDepositRate.getCode())));
        //异店消费费率
        sysSettlementRate.setOtherStoreRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementOtherStoreRate.getCode())));
        //支付宝费率
        sysSettlementRate.setAliPayRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementAliRate.getCode())));
        //微信费率
        sysSettlementRate.setWxPayRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementWXRate.getCode())));
        sysSettlementRate.setSourceType("02");
        sysSettlementRate.setOrgId(sysStore.getId());

        return baseMapper.insertSysSettlementRate(sysSettlementRate);
    }

    @Override
    public void insertAllRate() {
        SysSettlementRate store = new SysSettlementRate();
        //工厂结算费率
        store.setFactoryRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementFactoryRate.getCode())));
        //材料费率
        store.setMaterialRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementMaterialRate.getCode())));
        //管理费率
        store.setManageRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementManageRate.getCode())));
        //押金费率
        store.setDepositRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementDepositRate.getCode())));
        //异店消费费率
        store.setOtherStoreRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementOtherStoreRate.getCode())));
        //支付宝费率
        store.setAliPayRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementAliRate.getCode())));
        //微信费率
        store.setWxPayRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.StoreSettlementWXRate.getCode())));

        baseMapper.insertAllRateStore(store);




        SysSettlementRate factory = new SysSettlementRate();
        //材料费率
        factory.setMaterialRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementMaterialRate.getCode())));
        //管理费率
        factory.setManageRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementManageRate.getCode())));
        //押金费率
        factory.setDepositRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementDepositRate.getCode())));
        // 云洗费率
        factory.setFactoryRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementOnlineWashRate.getCode())));
//        //异店消费费率
//        factory.setOtherStoreRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementOtherStoreRate.getCode())));
//        //支付宝费率
//        factory.setAliPayRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementAliRate.getCode())));
//        //微信费率
//        factory.setWxPayRate(new BigDecimal(configService.selectConfigByKey(SysConfigKey.FactorySettlementWXRate.getCode())));
        baseMapper.insertAllRateFactory(factory);
    }
}
