package com.bonus.material.push.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.StringUtils;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.material.push.domain.CostPushBean;
import com.bonus.material.push.service.CostPushService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;

/**
 * @author : 阮世耀
 * @version : 1.0
 * @PackagePath: com.bonus.material.push.controller
 * @CreateTime: 2025-06-29  13:57
 * @Description: 描述
 */
@RequestMapping("/backstage/costPush/")
@RestController
public class CostPushController extends BaseController {

    private static final String KEY = "$jqgcYouote@c103";

    @Autowired
    private CostPushService service;

    /**
     * 查询费用推送审核--列表
     */
    @RequestMapping(value = "getCostPushCheckList", method = RequestMethod.GET)
    public TableDataInfo getCostPushCheckList(CostPushBean o) {
        try {
            List<CostPushBean> results = service.getCostPushCheckList(o);
            return getDataTable(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    /**
     * 查询费用推送审核--合计
     */
    @RequestMapping(value = "getCostPushCheckListCount", method = RequestMethod.GET)
    public AjaxResult getCostPushCheckListCount(CostPushBean o) {
        try {
            CostPushBean results = service.getCostPushCheckListCount(o);
            return success(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    /**
     * 查询费用推送审核--列表
     */
    @RequestMapping(value = "getCostPushLeaseList", method = RequestMethod.GET)
    public TableDataInfo getCostPushLeaseList(CostPushBean o) {
        try {
            List<CostPushBean> results = service.getCostPushLeaseList(o);
            return getDataTable(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }


    /**
     * 查询消耗费用推送审核--列表
     */
    @RequestMapping(value = "getConsumPushCheckList", method = RequestMethod.GET)
    public TableDataInfo getConsumPushCheckList(CostPushBean o) {
        try {
            List<CostPushBean> results = service.getConsumPushCheckList(o);
            return getDataTable(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    /**
     * 查询消耗费用推送审核--合计
     */
    @RequestMapping(value = "getConsumPushCheckListCount", method = RequestMethod.GET)
    public AjaxResult getConsumPushCheckListCount(CostPushBean o) {
        try {
            CostPushBean results = service.getConsumPushCheckListCount(o);
            return success(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    /**
     * 审核选中的数据
     */
    @RequestMapping(value = "checkData", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult checkData(String chks) {
        try {
            int res = createParams(chks,"1");
            if (res > 0) {
                new Thread(() -> service.pushDataToYouer(chks)).start();
            }
            return success("审核成功");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    /**
     * 消耗性费用审核选中的数据
     */
    @RequestMapping(value = "consumeCheckData", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult consumeCheckData(String chks) {
        try {
            int res = createParams(chks,"2");
            if (res > 0) {
                new Thread(() -> service.pushConsumeDataToYouer(chks)).start();
            }
            return success("审核成功");
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    private int createParams(String cheks,String type) {
        int res = 0;
        if (StringUtils.isNotEmpty(cheks)) {
            JSONArray object = JSONObject.parseArray(cheks);
            if (object != null && !object.isEmpty()) {
                for (int i = 0; i < object.size(); i++) {
                    CostPushBean bean = new CostPushBean();
                    JSONObject idObj = object.getJSONObject(i);
                    String proId = idObj.getString("projectId");
                    String taskId = idObj.getString("taskId");
                    String unitId = idObj.getString("unitId");
                    bean.setTaskId(taskId);
                    bean.setProjectId(proId);
                    bean.setUnitId(unitId);
                    if("1".equals(type)){
                        res = service.updatePushAQStatus(bean);
                    }
                    if("2".equals(type)){
                        res = service.updatePushXHStatus(bean);
                    }
                }
            }
        }
        return res;
    }


    /**
     * 费用审核导出
     */
    @RequestMapping("exportPushCheck")
    public void exportPushCheck(HttpServletResponse response, CostPushBean o) {
        try {
            String month = o.getMonth();
            if (StringUtils.isBlank(month)) {
                month = DateUtils.getCurrentMonth();
            }
            List<CostPushBean> results = service.getCostPushCheckList(o);
            ExcelUtil<CostPushBean> excelUtil = new ExcelUtil<>(CostPushBean.class);
            excelUtil.exportExcel(response, results, "费用推送审核表" + month + "月份");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }

    /**
     * 消耗品费用审核导出
     */
    @RequestMapping("exportConsPushCheck")
    public void exportConsPushCheck(HttpServletResponse response, CostPushBean o) {
        try {
            String month = o.getMonth();
            if (StringUtils.isBlank(month)) {
                month = DateUtils.getCurrentMonth();
            }
            List<CostPushBean> results = service.getConsumPushCheckList(o);
            ExcelUtil<CostPushBean> excelUtil = new ExcelUtil<>(CostPushBean.class);
            excelUtil.exportExcel(response, results, "消耗品费用推送审核表" + month + "月份");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }

    @RequestMapping(value = "findDetails", method = RequestMethod.POST)
    @ResponseBody
    public TableDataInfo findDetails(CostPushBean o) {
        try {
            List<CostPushBean> results = service.findDetails(o);
            return getDataTable(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            return getDataTable(Collections.emptyList());
        }
    }

    @RequestMapping(value = "findOtherDetails", method = RequestMethod.POST)
    @ResponseBody
    public TableDataInfo findOtherDetails(CostPushBean o) {
        try {
            List<CostPushBean> results = service.findOtherDetails(o);
            return getDataTable(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            return getDataTable(Collections.emptyList());
        }
    }

}
