package com.ruixuan.isc.controller;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.controller.BaseController;
import com.ruixuan.common.core.domain.AjaxResult;
import com.ruixuan.common.core.page.TableDataInfo;
import com.ruixuan.common.utils.Check;
import com.ruixuan.isc.entity.KuaishouRuleDetail;
import com.ruixuan.isc.entity.KuaishouSettlementRules;
import com.ruixuan.isc.service.IKuaishouSettlementRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/rules")
public class KuaishouSettlementRulesController extends BaseController {
    @Autowired
    private IKuaishouSettlementRulesService kuaishouSettlementRulesService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/getAllRuleList")
    public TableDataInfo getAllRuleList(Long userId, String ruleName) {
        startPage();
        if (userId == 1) {
            userId = null;
        }
        List<JSONObject> list = kuaishouSettlementRulesService.getAllRuleList(userId, ruleName);
        return getDataTable(list);
    }


    @GetMapping("/getRuleList")
    public TableDataInfo list(Long userId, String ruleName) {
        startPage();
        if (userId == 1) {
            userId = null;
        }
        List<JSONObject> list = kuaishouSettlementRulesService.getRuleList(userId, ruleName);
        return getDataTable(list);
    }

    @GetMapping("/getRuleListNoPage")
    public JSONObject getRuleListNoPage(Long userId, String ruleNam) {
        JSONObject returnJson = new JSONObject();
        if (userId == 1) {
            userId = null;
        }
        List<JSONObject> data = kuaishouSettlementRulesService.getRuleList(userId, ruleNam);
        returnJson.put("code", 0);
        returnJson.put("message", "true");
        returnJson.put("data", data);
        return returnJson;
    }


    @GetMapping("/getPromoterRuleList")
    public JSONObject getPromoterRuleList(Long userId, String ruleName) {
        JSONObject returnJson = new JSONObject();
        if (userId == 1) {
            userId = null;
        }
        List<JSONObject> data = kuaishouSettlementRulesService.getPromoterRuleList(userId, ruleName);
        returnJson.put("code", 0);
        returnJson.put("message", "true");
        returnJson.put("data", data);
        return returnJson;
    }

    @PostMapping("/addRule")
    public JSONObject addRule(@RequestBody JSONObject requestJson) {
        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(requestJson)) {
                throw new Exception("入参不能为空");
            }
            JSONArray ruleDetails = requestJson.getJSONArray("ruleDetails");
            if (Check.isNull(ruleDetails)) {
                throw new Exception("请输入规则详情");
            }

            if (requestJson.getInteger("ruleType") == 2) {
                if (ruleDetails.size() > 1) {
                    throw new Exception("按单数结算只能设置一条规则");
                }
            }
            if (requestJson.getInteger("ruleType") == 3) {
                if (ruleDetails.size() > 1) {
                    throw new Exception("按达人结算只能设置一条规则");
                }
            }

            kuaishouSettlementRulesService.addRules(requestJson);
            returnJson.put("code", 0);
            returnJson.put("message", "新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnJson.put("code", 500);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;
    }


    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody KuaishouSettlementRules kuaishouSettlementRules) {
        return toAjax(kuaishouSettlementRulesService.updateKuaishouSettlementRules(kuaishouSettlementRules));
    }


    @DeleteMapping("/remove")
    public AjaxResult remove(Long id) {
        return toAjax(kuaishouSettlementRulesService.deleteKuaishouSettlementRulesById(id));
    }

    @GetMapping("/getRuleDetailByRuleId")
    public JSONObject getRuleDetailByRuleId(Long ruleId) {
        JSONObject returnJson = new JSONObject();
        List<KuaishouRuleDetail> data = kuaishouSettlementRulesService.getRuleDetailByRuleId(ruleId);
        returnJson.put("code", 0);
        returnJson.put("message", "true");
        returnJson.put("data", data);
        return returnJson;
    }


}
