package cn.com.countrygarden.bi.main.controller.wcx;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dto.wcx.WcxInnovationDto;
import cn.com.countrygarden.bi.main.dto.wcx.WcxReportDto;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.handler.wcx.WcxReportBaselineExportHandle;
import cn.com.countrygarden.bi.main.handler.wcx.WcxReportProjectExportHandle;
import cn.com.countrygarden.bi.main.handler.wcx.WcxReportRegionExportHandle;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.sys.UserRoles;
import cn.com.countrygarden.bi.main.service.wcx.*;
import freemarker.core.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by ADMS on 2018/8/13.
 */
@RestController
@RequestMapping("/api/wcx/report")
public class WcxReportController {
    @Autowired
    private WcxInnovationService wcxInnovationService;
    @Autowired
    private WcxRoleService wcxRoleService;
    @Autowired
    private WcxUserService wcxUserService;
    @Autowired
    private WcxRegionProjectService wcxRegionProjectService;
    @Autowired
    private WcxReportService wcxReportService;
    @Autowired
    private WcxReportProjectExportHandle wcxReportProjectExportHandle;
    @Autowired
    private WcxReportRegionExportHandle wcxReportRegionExportHandle;
    @Autowired
    private WcxReportBaselineExportHandle wcxReportBaselineExportHandle;

    /**
     * 项目报表
     * @return
     */
    @GetMapping("/project/pageList")
    public Result projectReport(WcxInnovationDto condition, HttpServletRequest request){
        if (!StringUtils.hasText(condition.getStartDateStr())) {
            return Response.responseFail("开始时间不能为空");
        }
        if (!StringUtils.hasText(condition.getEndDateStr())) {
            return Response.responseFail("结束时间不能为空");
        }

        AccessToken accessToken = wcxUserService.getAccessToken(request);
        String userCode = accessToken.getUserCode();
        List<UserRoles> userRoles = wcxRoleService.selectSysRoleBySysUserCode(userCode);
        String maxRole = wcxRoleService.getMaxRole(userRoles);
        if ("0".equals(maxRole)) {
            String erpRegionName = wcxRegionProjectService.selectErpRegionName(userCode);
            erpRegionName = specialErpAndNcName(erpRegionName);
            condition.setRegionName(erpRegionName);
        }

        PageData pageData = wcxReportService.selectProjectReportPageData(condition);

        //wcxReportService.selectProjectReportPageDate();
       /* if ("Z".equals(maxRole)) {
        } else {

        }
*/
        //String erpRegionName = wcxRegionProjectService.selectErpRegionName(userCode);
        return Response.response(pageData);
    }

    /**
     * 区域报表
     * @return
     */
    @GetMapping("/region/pageList")
    public Result regionReport(WcxInnovationDto condition, HttpServletRequest request){
        if (!StringUtils.hasText(condition.getStartDateStr())) {
            return Response.responseFail("开始时间不能为空");
        }
        if (!StringUtils.hasText(condition.getEndDateStr())) {
            return Response.responseFail("结束时间不能为空");
        }

        AccessToken accessToken = wcxUserService.getAccessToken(request);
        String userCode = accessToken.getUserCode();
        List<UserRoles> userRoles = wcxRoleService.selectSysRoleBySysUserCode(userCode);
        String maxRole = wcxRoleService.getMaxRole(userRoles);
        if ("0".equals(maxRole)) {
            String erpRegionName = wcxRegionProjectService.selectErpRegionName(userCode);
            erpRegionName = specialErpAndNcName(erpRegionName);
            condition.setRegionName(erpRegionName);
        }

        PageData pageData = wcxReportService.selectRegionReportPageData(condition);

        //wcxReportService.selectProjectReportPageDate();
       /* if ("Z".equals(maxRole)) {
        } else {

        }
*/
        //String erpRegionName = wcxRegionProjectService.selectErpRegionName(userCode);
        return Response.response(pageData);
    }

    private String specialErpAndNcName(String erpRegionName) {
        // 特殊处理,NC组织体系与ERP组织体系, 目前通过名称关联, 但是NC的名称为广州东莞深区域,erp的名称为广州东/莞深区域,这里特殊处理区域信息
        if(StringUtils.hasText(erpRegionName)){
            if(erpRegionName.contains("广州东/莞深区域")){
                erpRegionName = "广州东莞深区域";
            }
            if(erpRegionName.contains("广清/佛山区域")){
                erpRegionName="广清佛山区域";
            }
            if(erpRegionName.contains("粤东/惠深区域")){
                erpRegionName="粤东惠深区域";
            }
            if(erpRegionName.contains("广清/佛山区域")){
                erpRegionName="广清佛山区域";
            }
            if(erpRegionName.contains("广西/云南区域")){
                erpRegionName="广西云南区域";
            }
            if(erpRegionName.contains("广清/莞深区域")){
                erpRegionName="广清莞深区域";
            }
        }
        return erpRegionName;
    }

    /**
     * 各基础业务线报表
     * @return
     */
    @GetMapping("/baseline/pageList")
    public Result baselineReport(WcxInnovationDto condition, HttpServletRequest request){
        if (!StringUtils.hasText(condition.getStartDateStr())) {
            return Response.responseFail("开始时间不能为空");
        }
        if (!StringUtils.hasText(condition.getEndDateStr())) {
            return Response.responseFail("结束时间不能为空");
        }

        AccessToken accessToken = wcxUserService.getAccessToken(request);
        String userCode = accessToken.getUserCode();
        List<UserRoles> userRoles = wcxRoleService.selectSysRoleBySysUserCode(userCode);
        String maxRole = wcxRoleService.getMaxRole(userRoles);
        if ("0".equals(maxRole)) {
            String erpRegionName = wcxRegionProjectService.selectErpRegionName(userCode);
            erpRegionName = specialErpAndNcName(erpRegionName);
            condition.setRegionName(erpRegionName);
        }

        PageData pageData = wcxReportService.selectBaselineReportPageData(condition);

        //wcxReportService.selectProjectReportPageDate();
       /* if ("Z".equals(maxRole)) {
        } else {

        }
*/
        //String erpRegionName = wcxRegionProjectService.selectErpRegionName(userCode);
        return Response.response(pageData);
    }

    /**
     * 项目报表导出
     *
     * @throws ParseException
     */
    @GetMapping("/project/export")
    public void regionProject(WcxInnovationDto condition, HttpServletRequest request, HttpServletResponse response) throws ParseException {
        //checkExportParam(condition);
        // 条件

        AccessToken accessToken = wcxUserService.getAccessToken(request);
        String userCode = accessToken.getUserCode();
        List<UserRoles> userRoles = wcxRoleService.selectSysRoleBySysUserCode(userCode);
        String maxRole = wcxRoleService.getMaxRole(userRoles);
        if ("0".equals(maxRole)) {
            String erpRegionName = wcxRegionProjectService.selectErpRegionName(userCode);
            erpRegionName = specialErpAndNcName(erpRegionName);
            condition.setRegionName(erpRegionName);
        }
        condition.setPageSize(-1);
        List<WcxReportDto> wcxReportDtos = wcxReportService.selectProjectReportList(condition);
        ServletOutputStream outputStream;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("获取io流失败");
        }
        // 响应处理
        response.setContentType("multipart/form-data");
        String fileName = "export.xlsx";
        try {
            fileName = URLEncoder.encode("微创新项目报表" + ".xlsx", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
        wcxReportProjectExportHandle.exportItem(wcxReportDtos, "微创新项目报表", outputStream);
    }

    /**
     * 区域报表导出
     *
     * @throws ParseException
     */
    @GetMapping("/region/export")
    public void exportByOne(WcxInnovationDto condition, HttpServletRequest request, HttpServletResponse response) throws ParseException {
        //checkExportParam(condition);

        // 条件
        AccessToken accessToken = wcxUserService.getAccessToken(request);
        String userCode = accessToken.getUserCode();
        List<UserRoles> userRoles = wcxRoleService.selectSysRoleBySysUserCode(userCode);
        String maxRole = wcxRoleService.getMaxRole(userRoles);
        if ("0".equals(maxRole)) {
            String erpRegionName = wcxRegionProjectService.selectErpRegionName(userCode);
            erpRegionName = specialErpAndNcName(erpRegionName);
            condition.setRegionName(erpRegionName);
        }
        condition.setPageSize(-1);
        List<WcxReportDto> wcxReportDtos = wcxReportService.selectRegionReportList(condition);

        ServletOutputStream outputStream;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("获取io流失败");
        }
        // 响应处理
        response.setContentType("multipart/form-data");
        String fileName = "export.xlsx";
        try {
            fileName = URLEncoder.encode("微创新区域报表" + ".xlsx", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
        wcxReportRegionExportHandle.exportItem(wcxReportDtos,"微创新区域报表", outputStream);
    }

    /**
     * 基础业务线报表导出
     *
     * @throws ParseException
     */
    @GetMapping("/baseline/export")
    public void baselineExport(WcxInnovationDto condition, HttpServletRequest request, HttpServletResponse response) throws ParseException {
        //checkExportParam(condition);

        // 条件
        AccessToken accessToken = wcxUserService.getAccessToken(request);
        String userCode = accessToken.getUserCode();
        List<UserRoles> userRoles = wcxRoleService.selectSysRoleBySysUserCode(userCode);
        String maxRole = wcxRoleService.getMaxRole(userRoles);
        if ("0".equals(maxRole)) {
            String erpRegionName = wcxRegionProjectService.selectErpRegionName(userCode);
            erpRegionName = specialErpAndNcName(erpRegionName);
            condition.setRegionName(erpRegionName);
        } else if(!"Z".equals(maxRole)){
            condition.setBaseline(maxRole);
        }
        condition.setPageSize(-1);
        List<WcxReportDto> wcxReportDtos = wcxReportService.selectBaselineReportList(condition);

        ServletOutputStream outputStream;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("获取io流失败");
        }
        // 响应处理
        response.setContentType("multipart/form-data");
        String fileName = "export.xlsx";
        try {
            fileName = URLEncoder.encode("微创新基础业务线报表" + ".xlsx", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
        wcxReportBaselineExportHandle.exportItem(wcxReportDtos,"微创新基础业务线报表", outputStream);
    }
}
