package cn.palmte.work.controller.backend;

import cn.palmte.work.model.Admin;
import cn.palmte.work.model.Project;
import cn.palmte.work.pojo.SpaceVO;
import cn.palmte.work.service.ProjectService;
import cn.palmte.work.service.SpaceService;
import cn.palmte.work.utils.FreeMarkerUtil;
import cn.palmte.work.utils.InterfaceUtil;
import cn.palmte.work.utils.Utils;
import cn.palmte.work.utils.excel.ExportUtils;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.activiti.engine.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.jfunc.common.db.bean.Page;
import top.jfunc.common.utils.CollectionUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/space")
public class SpaceController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ProjectController.class);

    @Autowired
    private SpaceService spaceService;

    @Autowired
    private ProjectService projectService;

    /**
     * 项目列表
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value = "keywords", required = false) String keywords,
                       @RequestParam(value = "certainty") int certainty,
                       @RequestParam(value = "certaintyStr", required = false) String certaintyStr,
                       @RequestParam(value = PAGE_NUMBER, defaultValue = DEFAULT_PAGE_NUMBER) int pageNumber,
                       @RequestParam(value = PAGE_SIZE, defaultValue = DEFAULT_PAGE_SIZE) int pageSize,
                       Map<String, Object> model) {
        //当前登录人的角色类型
        model.put("keywords", keywords);
        model.put("pageNumber", pageNumber);
        Admin admin = InterfaceUtil.getAdmin();
        model.put("adminId", admin.getId());
        model.put("admin", admin);
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo(keywords, model);
        if (certainty == 0) {
            if (certaintyStr.startsWith("A")) {
                certainty = 1;
            } else if (certaintyStr.startsWith("B")) {
                certainty = 2;
            } else if (certaintyStr.startsWith("C")) {
                certainty = 3;
            }
        }
        model.put("certainty", certainty);
        searchInfo.putIfAbsent("certainty", String.valueOf(certainty));
        searchInfo.putIfAbsent("deptName", "销售管理部");
        searchInfo.putIfAbsent("estimateStatus", String.valueOf(1));
        Page<Project> page = spaceService.list(searchInfo, pageNumber, pageSize);
        List<Project> list = page.getList();
        if(CollectionUtil.isNotEmpty(list)){

            int offset = (pageNumber - 1) * pageSize;
            for (int i = 0 , size = list.size(); i < size; i++) {
                list.get(i).setTempId(i+1 + offset);
            }
        }
        model.put("pager", page);
        model.put("Utils", FreeMarkerUtil.fromStaticPackage("cn.palmte.work.utils.Utils"));
        return "admin/space_list";
    }

    /**
     * 项目导出
     */
    @RequestMapping("/export")
    public void export(@RequestParam(value = "keywords", required = false) String keywords, @RequestParam(value = "certainty") int certainty, HttpServletResponse httpServletResponse) throws IOException {
        Map<String, String> searchInfo = getSearchInfo(keywords);
        searchInfo.putIfAbsent("certainty", String.valueOf(certainty));
        searchInfo.putIfAbsent("deptName", "销售管理部");
        searchInfo.putIfAbsent("estimateStatus", String.valueOf(1));
        downloadHeader(httpServletResponse, Utils.generateExcelName("项目报表"), "application/octet-stream");
        String[] headers = {"项目编号", "项目名称", "项目类型", "垫资模式", "华智产品金额", "华三产品金额", "汇智产品金额", "其他产品金额", "项目把握度", "项目计划招标时间", "预计合同签订时间",
                "计收计划", "项目毛利", "合同金额", "项目解决方案", "具体解决方案", "是否二次签单", "最终用户名称", "客户名称", "紫光汇智直接投标/集成商转签", "负责人", "备注","项目阶段"};
        String[] exportColumns = {"projectNo", "name", "typeDesc", "underwrittenModeStr", "huazhiRound", "huasanRound", "huizhiRound", "ziguangRound", "certaintyStr", "bidsTime", "contractTime",
                "calculationCollection", "grossProfitRound", "contractRound", "resolvePlanStr", "mainContractResolvePlan", "isSecondStr", "terminalCustomer", "customer", "signTypeStr", "principal", "remark","stageName"};
        ExportUtils.exportToExcel(headers, exportColumns, 1, 10000,
                httpServletResponse.getOutputStream(), (pN, pS) -> spaceService.list(searchInfo, pN, pS).getList());
    }

    /**
     * 跳转页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/statistics")
    public String statistics(Map<String, Object> model) {

        return "/admin/space_statistics";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public String listData() {

        SpaceVO spaceVO = spaceService.getListData();
        return JSONObject.toJSONString(spaceVO);
    }
}
