package cn.com.ctop.kuaishou.modules.report.service.impl;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.report.mapper.SalesBudgetMapper;
import cn.com.ctop.kuaishou.modules.report.service.ISalesBudgetService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SalesBudgetServiceImpl implements ISalesBudgetService {
    @Autowired
    private SalesBudgetMapper salesBudgetMapper;

    @Override
    public Result<Object> bytedanceSalesReport(String saleId, Long statDate, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> data = salesBudgetMapper.bytedanceSalesReport(saleId, statDate);
        if (!Check.isNull(data)) {
            for (int i = 0; i < data.size(); i++) {
                JSONObject jsonObject = data.get(i);
                Long projectId = jsonObject.getLong("projectId");
                List<JSONObject> configs = salesBudgetMapper.getConfigs(projectId);
                if (!Check.isNull(configs)) {
                    JSONArray costArray = new JSONArray();
                    boolean trueOrFalse = false;
                    for (int j = 0; j < configs.size(); j++) {
                        JSONObject configJson = configs.get(j);
                        String ocpxCode = configJson.getString("ocpxCode");
                        String ocpxName = configJson.getString("ocpxName");
                        BigDecimal maxValue = configJson.getBigDecimal("maxValue");
                        if ("budget".equals(ocpxCode)) {
                            trueOrFalse = true;
                            BigDecimal budget = jsonObject.getBigDecimal("budget");
                            JSONObject budgetJson = new JSONObject();
                            budgetJson.put("setBudget", maxValue);
                            budgetJson.put("actualBudget", budget);
                            BigDecimal subtract = budget.subtract(maxValue);
                            int compare = subtract.compareTo(new BigDecimal(0));
                            budgetJson.put("compare", compare);
                            if (compare < 0) {
                                budgetJson.put("subtract", subtract.multiply(new BigDecimal(-1)));
                            } else if (compare == 0) {
                                budgetJson.put("subtract", 0);
                            } else {
                                budgetJson.put("subtract", subtract);
                            }


                            jsonObject.put("budgetJson", budgetJson);
                        } else {
                            JSONObject costJson = new JSONObject();
                            BigDecimal cost = jsonObject.getBigDecimal(ocpxCode);
                            if (Check.isNull(cost)) {
                                cost = new BigDecimal(0);
                            }
                            costJson.put("cost", cost);
                            costJson.put("setCost", maxValue);
                            costJson.put("name", ocpxName.replace("成本", ""));
                            BigDecimal subtract = new BigDecimal(0);
                            if (!Check.isNull(maxValue)) {
                                subtract = cost.subtract(maxValue);
                            }
                            int compare = subtract.compareTo(new BigDecimal(0));
                            costJson.put("compare", compare);
                            costArray.add(costJson);
                        }
                        if (!trueOrFalse) {
                            JSONObject budgetJson = new JSONObject();
                            BigDecimal budget = jsonObject.getBigDecimal("budget");
                            budgetJson.put("actualBudget", budget);
                            budgetJson.put("setBudget", "不限");
                            jsonObject.put("budgetJson", budgetJson);
                        }
                    }

                    jsonObject.put("costArray", costArray);
                } else {
                    jsonObject.put("costArray", new JSONArray());

                    JSONObject budgetJson = new JSONObject();
                    BigDecimal budget = jsonObject.getBigDecimal("budget");
                    budgetJson.put("actualBudget", budget);
                    jsonObject.put("budgetJson", budgetJson);

                }
            }
        }
        return Result.ok(new PageInfo<>(data));
    }

    @Override
    public Result<Object> kuaiShouSalesReport(String saleId, Long statDate, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> data = salesBudgetMapper.kuaiShouSalesReport(saleId, statDate);
        if (!Check.isNull(data)) {
            for (int i = 0; i < data.size(); i++) {
                JSONObject jsonObject = data.get(i);
                Long projectId = jsonObject.getLong("projectId");
                List<JSONObject> configs = salesBudgetMapper.getConfigs(projectId);
                if (!Check.isNull(configs)) {
                    JSONArray costArray = new JSONArray();
                    boolean trueOrFalse = false;
                    for (int j = 0; j < configs.size(); j++) {
                        JSONObject configJson = configs.get(j);
                        String ocpxCode = configJson.getString("ocpxCode");
                        String ocpxName = configJson.getString("ocpxName");
                        BigDecimal maxValue = configJson.getBigDecimal("maxValue");
                        if ("budget".equals(ocpxCode)) {
                            trueOrFalse = true;
                            BigDecimal budget = jsonObject.getBigDecimal("budget");
                            JSONObject budgetJson = new JSONObject();
                            budgetJson.put("setBudget", maxValue);
                            budgetJson.put("actualBudget", budget);
                            BigDecimal subtract = budget.subtract(maxValue);
                            int compare = subtract.compareTo(new BigDecimal(0));
                            budgetJson.put("compare", compare);
                            if (compare < 0) {
                                budgetJson.put("subtract", subtract.multiply(new BigDecimal(-1)));
                            } else if (compare == 0) {
                                budgetJson.put("subtract", 0);
                            } else {
                                budgetJson.put("subtract", subtract);
                            }
                            jsonObject.put("budgetJson", budgetJson);
                        } else {
                            JSONObject costJson = new JSONObject();
                            BigDecimal cost = jsonObject.getBigDecimal(ocpxCode);
                            if (Check.isNull(cost)) {
                                cost = new BigDecimal(0);
                            }
                            costJson.put("cost", cost);
                            costJson.put("setCost", maxValue);
                            costJson.put("name", ocpxName.replace("成本", ""));
                            BigDecimal subtract = new BigDecimal(0);
                            if (!Check.isNull(maxValue)) {
                                subtract = cost.subtract(maxValue);
                            }
                            int compare = subtract.compareTo(new BigDecimal(0));
                            costJson.put("compare", compare);
                            costArray.add(costJson);
                        }
                        if (!trueOrFalse) {
                            JSONObject budgetJson = new JSONObject();
                            BigDecimal budget = jsonObject.getBigDecimal("budget");
                            budgetJson.put("actualBudget", budget);
                            budgetJson.put("setBudget", "不限");
                            jsonObject.put("budgetJson", budgetJson);
                        }
                    }
                    jsonObject.put("costArray", costArray);
                } else {
                    jsonObject.put("costArray", new JSONArray());

                    JSONObject budgetJson = new JSONObject();
                    BigDecimal budget = jsonObject.getBigDecimal("budget");
                    budgetJson.put("actualBudget", budget);
                    jsonObject.put("budgetJson", budgetJson);

                }
            }

        }

        return Result.ok(new PageInfo<>(data));
    }

    @Override
    public List<JSONObject> ocpxList(String mediaId) {
        return salesBudgetMapper.ocpxList(mediaId);
    }

    @Override
    public void deleteInfo(Long projectId) {
        salesBudgetMapper.deleteInfo(projectId);
    }

    @Override
    public void adds(List<JSONObject> addList) {
        salesBudgetMapper.adds(addList);
    }

    @Override
    public JSONObject getById(Long projectId) {
        JSONObject json = new JSONObject();

        BigDecimal budget = salesBudgetMapper.getBudget(projectId);
        json.put("budget", budget);
        List<JSONObject> ruleDetails = salesBudgetMapper.getBudgetList(projectId);
        json.put("ruleDetails", ruleDetails);

        return json;
    }
}
