package com.fs.company.service.impl;

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

import com.alibaba.fastjson.JSON;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.StringUtils;
import com.fs.company.domain.CompanyDivConfig;
import com.fs.company.mapper.CompanyDivConfigMapper;
import com.fs.company.param.CompanyAcctInfo;
import com.fs.company.param.CompanyDivConfigUpdateParam;
import com.fs.company.service.ICompanyDivConfigService;
import com.fs.company.vo.CompanyDivConfigVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 公司分账配置Service业务层处理
 *
 * @author fs
 * @date 2025-10-20
 */
@Service
public class CompanyDivConfigServiceImpl extends ServiceImpl<CompanyDivConfigMapper, CompanyDivConfig> implements ICompanyDivConfigService {
    @Autowired
    private CompanyDivConfigMapper companyDivConfigMapper;

    /**
     * 查询公司分账配置
     *
     * @param companyId 公司分账配置主键
     * @return 公司分账配置
     */
    @Override
    public CompanyDivConfigVo selectCompanyDivConfigByCompanyId(Long companyId)
    {
        CompanyDivConfig companyDivConfig = baseMapper.selectCompanyDivConfigByCompanyId(companyId);
        if (companyDivConfig == null){
            return null;
        }
        CompanyDivConfigVo vo = new CompanyDivConfigVo();
        BeanUtils.copyProperties(companyDivConfig, vo);
        String acctInfosStr = companyDivConfig.getAcctInfos();
        if (StringUtils.isNotBlank(acctInfosStr)){
            List<CompanyAcctInfo> acctInfos = JSON.parseArray(acctInfosStr, CompanyAcctInfo.class);
            vo.setAcctInfos(acctInfos);
        }
        return vo;
    }

    /**
     * 查询公司分账配置列表
     *
     * @param companyDivConfig 公司分账配置
     * @return 公司分账配置
     */
    @Override
    public List<CompanyDivConfig> selectCompanyDivConfigList(CompanyDivConfig companyDivConfig)
    {
        return baseMapper.selectCompanyDivConfigList(companyDivConfig);
    }

    /**
     * 新增公司分账配置
     *
     * @param companyDivConfig 公司分账配置
     * @return 结果
     */
    @Override
    public int insertCompanyDivConfig(CompanyDivConfig companyDivConfig)
    {
        companyDivConfig.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertCompanyDivConfig(companyDivConfig);
    }

    /**
     * 修改公司分账配置
     *
     * @param companyDivConfig 公司分账配置
     * @return 结果
     */
    @Override
    public int updateCompanyDivConfig(CompanyDivConfig companyDivConfig)
    {
        companyDivConfig.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateCompanyDivConfig(companyDivConfig);
    }

    /**
     * 批量删除公司分账配置
     *
     * @param companyIds 需要删除的公司分账配置主键
     * @return 结果
     */
    @Override
    public int deleteCompanyDivConfigByCompanyIds(Long[] companyIds)
    {
        return baseMapper.deleteCompanyDivConfigByCompanyIds(companyIds);
    }

    /**
     * 删除公司分账配置信息
     *
     * @param companyId 公司分账配置主键
     * @return 结果
     */
    @Override
    public int deleteCompanyDivConfigByCompanyId(Long companyId)
    {
        return baseMapper.deleteCompanyDivConfigByCompanyId(companyId);
    }

    @Override
    public R setDiv(CompanyDivConfigUpdateParam param) {
        if (param.getCompanyId() == null || StringUtils.isBlank(param.getCompanyName())){
            return R.error("公司信息不能为空！");
        }
        Integer divFlag = param.getDivFlag();
        String acctInfosStr = null;
        //分账账户
        List<CompanyAcctInfo> acctInfos = param.getAcctInfos();
        if (divFlag == null){
            divFlag = 0;
        } else if (divFlag == 1){
            String delayAcctFlag = param.getDelayAcctFlag();
            if (StringUtils.isBlank(delayAcctFlag)){
                return R.error("分账模式不能为空！");
            }

            if (acctInfos == null || acctInfos.isEmpty()){
                return R.error("分账接收方不能为空！");
            }
            acctInfosStr = JSON.toJSONString(acctInfos);
        }
        CompanyDivConfig companyDivConfig = new CompanyDivConfig();
        BeanUtils.copyProperties(param, companyDivConfig);
        companyDivConfig.setAcctInfos(acctInfosStr);
        String percentageFlag = companyDivConfig.getPercentageFlag();
        if (acctInfos != null && !acctInfos.isEmpty()){
            if (StringUtils.isNotBlank(percentageFlag) && percentageFlag.equals("Y")){
                if (StringUtils.isBlank(companyDivConfig.getIsCleanSplit())){
                    companyDivConfig.setIsCleanSplit("N");
                }
            }
            Float total = 0f;
            for (CompanyAcctInfo acctInfo : acctInfos) {
                if ("N".equals(percentageFlag)){
                    if (BigDecimal.valueOf(0.01).compareTo(acctInfo.getDivAmt())>0) {
                        return R.error("分账接收方:"+acctInfo.getHuifuId()+"分账金额必须大于0.01！");
                    }
                } else if ("Y".equals(percentageFlag)){
                    if (acctInfo.getPercentageDiv()>100) {
                        return R.error("分账接收方:"+acctInfo.getHuifuId()+"分账百分比不能大于100！");
                    }
                    total = total + acctInfo.getPercentageDiv();
                    if (total>100){
                        return R.error("分账接收方百分比之和不能大于100！");
                    }
                }
            }
        }
        int flag = 0;
        if (param.getIsAdd()!= null && param.getIsAdd() == 1){
            //新增
            companyDivConfig.setCreateTime(DateUtils.getNowDate());
            flag = companyDivConfigMapper.insertCompanyDivConfig(companyDivConfig);
        } else {
            //更新
            companyDivConfig.setUpdateTime(DateUtils.getNowDate());
            flag = companyDivConfigMapper.updateCompanyDivConfig(companyDivConfig);
        }

        return flag>0?R.ok():R.error();
    }
}
