package com.ruixuan.isc.service.impl;

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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.utils.Check;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.isc.entity.KuaishouRuleDetail;
import com.ruixuan.isc.entity.KuaishouSettlementRules;
import com.ruixuan.isc.mapper.KuaishouRuleDetailMapper;
import com.ruixuan.isc.mapper.KuaishouSettlementRulesMapper;
import com.ruixuan.isc.service.IKuaishouSettlementRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-15
 */
@Service
public class KuaishouSettlementRulesServiceImpl implements IKuaishouSettlementRulesService {
    @Autowired
    private KuaishouSettlementRulesMapper kuaishouSettlementRulesMapper;
    @Autowired
    private KuaishouRuleDetailMapper kuaishouRuleDetailMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public KuaishouSettlementRules selectKuaishouSettlementRulesById(Long id) {
        return kuaishouSettlementRulesMapper.selectKuaishouSettlementRulesById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param kuaishouSettlementRules 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<KuaishouSettlementRules> selectKuaishouSettlementRulesList(KuaishouSettlementRules kuaishouSettlementRules) {
        return kuaishouSettlementRulesMapper.selectKuaishouSettlementRulesList(kuaishouSettlementRules);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param kuaishouSettlementRules 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertKuaishouSettlementRules(KuaishouSettlementRules kuaishouSettlementRules) {
        kuaishouSettlementRules.setCreateTime(DateUtils.getNowDate());
        return kuaishouSettlementRulesMapper.insertKuaishouSettlementRules(kuaishouSettlementRules);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param kuaishouSettlementRules 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateKuaishouSettlementRules(KuaishouSettlementRules kuaishouSettlementRules) {
        kuaishouSettlementRules.setUpdateTime(DateUtils.getNowDate());
        return kuaishouSettlementRulesMapper.updateKuaishouSettlementRules(kuaishouSettlementRules);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouSettlementRulesByIds(Long[] ids) {
        return kuaishouSettlementRulesMapper.deleteKuaishouSettlementRulesByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteKuaishouSettlementRulesById(Long id) {
        kuaishouRuleDetailMapper.deleteKuaishouRuleDetailByRuleId(id);
        return kuaishouSettlementRulesMapper.deleteKuaishouSettlementRulesById(id);
    }

    @Override
    public List<JSONObject> getRuleList(Long userId, String ruleName) {
        return kuaishouSettlementRulesMapper.getRuleList(userId, ruleName);
    }

    @Override
    public void addRules(JSONObject requestJson) {
        Long maxRuleId = kuaishouSettlementRulesMapper.getMaxRuleId();
        Long newId = maxRuleId + 1;
        String ruleName = requestJson.getString("ruleName");
        Integer ruleType = requestJson.getInteger("ruleType");
        Long userId = requestJson.getLong("userId");
        String userName = requestJson.getString("userName");
        KuaishouSettlementRules settlementRules = new KuaishouSettlementRules();
        settlementRules.setId(newId);
        settlementRules.setRuleName(ruleName);
        settlementRules.setRuleType(ruleType);
        settlementRules.setUserId(userId);
        settlementRules.setUserName(userName);
        int i = kuaishouSettlementRulesMapper.insertKuaishouSettlementRules(settlementRules);
        if (i > 0) {
            List<KuaishouRuleDetail> details = new ArrayList<>();
                JSONArray ruleDetails = requestJson.getJSONArray("ruleDetails");
                for (int j = 0; j < ruleDetails.size(); j++) {
                    JSONObject jsonObject = ruleDetails.getJSONObject(j);
                    KuaishouRuleDetail detail = new KuaishouRuleDetail();
                    detail.setRuleId(newId);
                    Integer startItemCount = jsonObject.getInteger("startItemCount");
                    Integer endItemCount = jsonObject.getInteger("endItemCount");
                    BigDecimal anchorAmount = jsonObject.getBigDecimal("anchorAmount");
                    BigDecimal referenceAmount = jsonObject.getBigDecimal("referenceAmount");
                    Integer sortCount = jsonObject.getInteger("sortCount");
                    detail.setStartItemCount(startItemCount);
                    detail.setEndItemCount(endItemCount);
                    detail.setAnchorAmount(anchorAmount);
                    detail.setReferenceAmount(referenceAmount);
                    detail.setSortCount(sortCount);
                    details.add(detail);
                }
            if (!Check.isNull(details)) {
                kuaishouRuleDetailMapper.addAll(details);
            }
        }
    }

    @Override
    public List<KuaishouRuleDetail> getRuleDetailByRuleId(Long ruleId) {
        return kuaishouRuleDetailMapper.getRuleDetailByRuleId(ruleId);
    }

    @Override
    public List<JSONObject> getPromoterRuleList(Long userId, String ruleName) {
        return kuaishouRuleDetailMapper.getPromoterRuleList(userId,ruleName);
    }

    @Override
    public List<JSONObject> getAllRuleList(Long userId, String ruleName) {
        return kuaishouRuleDetailMapper.getAllRuleList(userId,ruleName);
    }
}

