package com.gree.mall.manager.logic.settle.repair;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.settle.repair.DailyWithholdVO;
import com.gree.mall.manager.commonmapper.CommonMapper;
import com.gree.mall.manager.constant.SysDictConstant;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.SettleDailyWithhold;
import com.gree.mall.manager.plus.entity.SysDictCompany;
import com.gree.mall.manager.plus.service.SettleDailyWithholdService;
import com.gree.mall.manager.plus.service.SysDictCompanyService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Slf4j
@RequiredArgsConstructor
public class DailyWithholdLogic {

    private final CommonLogic commonLogic;
    private final CommonMapper commonMapper;
    private final SettleDailyWithholdService settleDailyWithholdService;
    private final SysDictCompanyService sysDictCompanyService;

    public IPage<DailyWithholdVO> page(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        FieldUtils.supplyParam(zfireParamBean, DailyWithholdVO.class, adminUser);

        return commonMapper.repairSettleDailyWithholdList(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
    }

    public SettleDailyWithhold detail(String id) {
        return settleDailyWithholdService.getById(id);
    }

    public void add(SettleDailyWithhold dailyWithhold) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        if (adminUser.getType() == 2) {
            throw new RemoteServiceException("平台账号禁止操作");
        }

        dailyWithhold.setCompanyWechatId(adminUser.getAdminCompanyWechat().getCompanyWechatId())
                .setCompanyWechatName(adminUser.getAdminCompanyWechat().getCompanyName())
                .insert();
    }


    public void update(SettleDailyWithhold dailyWithhold) {
        dailyWithhold.updateById();
    }


    public void delete(String id) {
        settleDailyWithholdService.removeById(id);
    }

    public SysDictCompany getConfig() {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        if (Objects.isNull(adminUser.getAdminCompanyWechat())) {
            throw new RemoteServiceException("平台账号获取不到对应商户”结算汇总暂扣款比例“配置");
        }

        SysDictCompany dict = sysDictCompanyService.lambdaQuery()
                .eq(SysDictCompany::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                .eq(SysDictCompany::getDictType, SysDictConstant.REPAIR_DAILY)
                .last("limit 1")
                .one();

        if (Objects.isNull(dict)) {
            dict = new SysDictCompany();
            dict.setDictType(SysDictConstant.REPAIR_DAILY);
            dict.setDictCode(IdWorker.getIdStr());
            dict.setDictValue("0");
            dict.setRemark("维修日结算扣款配置-结算汇总暂扣款比例");
            dict.setCompanyWechatId(adminUser.getAdminCompanyWechat().getCompanyWechatId());
            dict.setCompanyName(adminUser.getAdminCompanyWechat().getCompanyWechatId());
        }

        return dict;
    }

    @Transactional
    public void saveConfig(BigDecimal rate) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        if (adminUser.getType() == 2) {
            throw new RemoteServiceException("平台账号禁止操作");
        }

        if (rate.doubleValue() > 1 || rate.doubleValue() < 0) {
            throw new RemoteServiceException("不可大于100%,且必须为正数");
        }
        sysDictCompanyService.lambdaUpdate()
                .eq(SysDictCompany::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                .eq(SysDictCompany::getDictType, SysDictConstant.REPAIR_DAILY)
                .remove();

        SysDictCompany dict = new SysDictCompany();
        dict.setDictType(SysDictConstant.REPAIR_DAILY);
        dict.setDictCode(IdWorker.getIdStr());
        dict.setDictValue(String.valueOf(rate));
        dict.setRemark("维修日结算扣款配置-结算汇总暂扣款比例");
        dict.setCompanyWechatId(adminUser.getAdminCompanyWechat().getCompanyWechatId());
        dict.setCompanyName(adminUser.getAdminCompanyWechat().getCompanyName());


        dict.insert();
    }

    public List<SysDictCompany> getConfig(String code) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        if (adminUser.getType() == 2) {
            throw new RemoteServiceException("平台账号禁止操作");
        }

        return sysDictCompanyService.lambdaQuery()
                .eq(SysDictCompany::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                .eq(SysDictCompany::getDictType, code)
                .list();
    }
}
