package org.jeecg.ctop.material.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecg.common.api.vo.Result;
import org.jeecg.ctop.material.constants.Check;
import org.jeecg.ctop.material.constants.ExportExcelUtils;
import org.jeecg.ctop.material.entity.ManagerSaleValue;
import org.jeecg.ctop.material.service.IManagerSaleValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 分公司总经理&销售总监阶段性目标值
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2022-04-14
 */
@Slf4j
@Api(tags = "分公司总经理&销售总监阶段性目标值")
@RestController
@RequestMapping("/manager/sale")
public class ManagerSaleValueController {
    private static final Logger log = LoggerFactory.getLogger(ManagerSaleValueController.class);

    @Autowired
    private IManagerSaleValueService managerSaleValueService;

    /**
     * 查询人员权限和公司
     */
    @GetMapping(value = "/getCompany")
    public Result<Object> getCompany(String userId) {
        if (Check.isNull(userId)) {
            return Result.error("缺少参数");
        }
        try {
            return managerSaleValueService.getCompany(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }


    /**
     * 查询当日消耗 + 月度消耗
     */
    @GetMapping(value = "/getDayAndMonthlyCharges")
    public Result<Object> getDayAndMonthlyCharges(String userId, String mediaId, String startTime, String endTime, String operate) {
        if (Check.isNull(userId) || Check.isNull(mediaId)) {
            return Result.error("缺少参数");
        }
        try {
            return managerSaleValueService.getDayAndMonthlyCharges(userId, mediaId, startTime, endTime, operate);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 设置本月消耗完成度
     */
    @PostMapping(value = "/setUpManagerSaleValue")
    public Result<Object> setUpManagerSaleValue(@RequestBody ManagerSaleValue sale) {
        if (Check.isNull(sale.getMediaId()) || Check.isNull(sale.getUserId()) || (Check.isNull(sale.getWeeklyGoalValue()) && Check.isNull(sale.getMonthlyGoalValue()))) {
            return Result.error("缺少参数");
        }
        try {
            managerSaleValueService.setUpManagerSaleValue(sale);
            return Result.ok("success");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("修改失败");
        }
    }

    /**
     * 查询home页消耗+平台消耗
     */
    @GetMapping(value = "/getHomeCharges")
    public Result<Object> getHomeCharges(String userId, String mediaId, String startTime, String endTime) {
        if (Check.isNull(userId) || Check.isNull(mediaId) || Check.isNull(startTime) || Check.isNull(endTime)) {
            return Result.error("缺少参数");
        }
        try {
            return managerSaleValueService.getHomeCharges(userId, mediaId, startTime, endTime);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 项目消耗排行列表
     */
    @GetMapping(value = "/getProjectChargeTop")
    public Result<Object> getProjectChargeTop(String userId, String mediaId, String startTime, String endTime, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, String operate) {
        if (Check.isNull(userId) || Check.isNull(mediaId) || Check.isNull(startTime) || Check.isNull(endTime)) {
            return Result.error("缺少参数");
        }
        try {
            PageInfo page = managerSaleValueService.getProjectChargeTop(userId, mediaId, startTime, endTime, operate, pageNo, pageSize);
            return Result.ok(page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败", e.getMessage());
        }
    }

    /**
     * 项目消耗排行列表
     */
    @GetMapping(value = "/getProjectChargeExcel")
    public void getProjectChargeExcel(HttpServletResponse response, String userId, String mediaId, String startTime, String endTime, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, String operate) {
        try {

            PageInfo page = managerSaleValueService.getProjectChargeTop(userId, mediaId, startTime, endTime, operate, pageNo, 9999999);
            List<JSONObject> list = page.getList();
            List<List<Object>> excelList = new ArrayList<>();
            if (!list.isEmpty()) {
                list.forEach(data -> {
                    List<Object> temp = new ArrayList<>();
                    temp.add(data.getString("projectName"));
                    temp.add(data.getString("operateName"));
                    temp.add(data.getString("saleName"));
                    temp.add(data.getString("charge"));
                    temp.add(data.getString("chargeRoi"));
                    temp.add(data.getString("realCharge"));
                    temp.add(data.getString("jlCharge"));
                    temp.add(data.getString("newUnitNum"));
                    excelList.add(temp);
                });
            }
            List<String> titles = new ArrayList<>();
            titles.add(0, "项目");
            titles.add(1, "运营负责人");
            titles.add(2, "销售");
            titles.add(3, "消耗(元)");
            titles.add(4, "消耗环比");
            titles.add(5, "现金消耗");
            titles.add(6, "激励消耗");
            titles.add(7, "项目基建数");

            OutputStream os = response.getOutputStream();
            ExportExcelUtils eeu = new ExportExcelUtils();
            XSSFWorkbook workbook = new XSSFWorkbook();
            String[] headers = titles.toArray(new String[titles.size()]);
            eeu.exportExcelForAttendance(workbook, 0, "项目消耗", headers, excelList);
            workbook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询失败", e.getMessage());
        }
    }


}
