package cn.com.countrygarden.bi.main.controller.extension.v1;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.constant.ExtensionContent;
import cn.com.countrygarden.bi.main.dto.extension.ExtensionDupDTO;
import cn.com.countrygarden.bi.main.dto.extension.ExtensionErpDTO;
import cn.com.countrygarden.bi.main.dto.extension.ExtensionPageData;
import cn.com.countrygarden.bi.main.dto.extension.ExtensionReportDTO;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.extension.ExtensionDupBindingService;
import cn.com.countrygarden.bi.main.service.extension.ExtensionErpBindingService;
import cn.com.countrygarden.bi.main.service.extension.ExtensionReportService;
import cn.com.countrygarden.bi.main.util.ExtensionReportUtils;
import cn.com.countrygarden.bi.main.util.OSSUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName ExtensionReportController
 * @Description 市场拓展 -> 外拓报表-> 控制层
 * @Author Sjiang
 * @Date 2019/1/18 15:49
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/extension/v1")
public class ExtensionReportController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ExtensionReportService reportService;
    @Autowired
    private ExtensionErpBindingService erpBindingService;
    @Autowired
    private ExtensionDupBindingService dupBindingService;
    @Autowired
    private AccessTokenService accessTokenService;
    @Autowired
    private OSSUtils ossUtils;

    /**
     * @return java.lang.Object
     * @Author Sjiang
     * @Description 获取所有已绑定报表信息
     * @Date 15:53 2019/1/18
     * @Param [reportDTO]
     **/
    @PostMapping("/report/binding/list")
    public Object reportBinDingList(ExtensionReportDTO reportDTO, ExtensionPageData pageData, HttpServletRequest request) {
        try {
            logger.info("用户请求查询所有已绑定报表信息");
            return Response.response(reportService.selectAll(reportDTO, accessTokenService, request, pageData));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        }
    }

    /**
     * @return java.lang.Object
     * @Author Sjiang
     * @Description 获取外拓报表信息
     * @Date 15:07 2019/1/21
     * @Param []
     **/

    @PostMapping("/report/list")
    public Object extensionReportList(ExtensionReportDTO reportDTO, ExtensionPageData pageData, HttpServletRequest request) {
        try {
            logger.info("用户请求查询外拓报表信息");
            return Response.response(reportService.selectReportAll(reportDTO, accessTokenService, request, pageData));
        } catch (ServiceException e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail("查询外拓报表信息失败！");
        }
    }

    /**
     * @return java.lang.Object
     * @Author Sjiang
     * @Description ERP 、DUP 信息进行绑定
     * @Date 9:30 2019/1/22
     * @Param [reportDTO]
     **/
    @PostMapping("/report/binding")
    public Object dataBinding(@RequestBody ExtensionReportDTO reportDTO, HttpServletRequest request) {

        try {

            logger.info("用户进行信息绑定！");
            return Response.response(reportService.dataBinding(reportDTO, ExtensionReportUtils.getUserCode(accessTokenService, request)));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        }
    }
    /**
     * @Author sjiang
     * @Description  更新已绑定的 erp 信息
     * @Date 16:57 2019/5/21
     * @Param [erpId, request]
     * @return java.lang.Object
     **/
    @PostMapping("/report/binding/update")
    public Object dataBindingUpdate(@RequestParam(required = true) Long erpId, HttpServletRequest request) {
        try {
            logger.info("用户进行信息绑定！");
            return Response.response(reportService.dataBindingUpdate(erpId, ExtensionReportUtils.getUserCode(accessTokenService, request)));
        } catch (ServiceException e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail("网络错误，更新失败，请稍后重试！");
        }
    }

    /**
     * @return
     * @Author Sjiang
     * @Description //  ERP 数据
     * @Date 10:18 2019/1/15
     * @Param
     **/
    @PostMapping(value = "/erp/list")
    public Object erpList(ExtensionErpDTO erpDTO, ExtensionPageData pageData, HttpServletRequest request) {
        try {
            if (StringUtils.isNotBlank(erpDTO.getBindingIdentifier()) && ExtensionContent.BINDING_IDENTIFIER_All.equals(erpDTO.getBindingIdentifier())) {
                logger.info("用户查询已录入 ERP 信息");
                return Response.response(erpBindingService.selectAll(erpDTO,accessTokenService, request, pageData));
            } else {
                logger.info("用户查询绑定或未绑定 ERP 信息");
                return Response.response(erpBindingService.selectErpBindingOrUnbinding(erpDTO,accessTokenService, request, pageData));
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        }
    }

    /**
     * @return
     * @Author Sjiang
     * @Description //  DUP 数据,包括未绑定 已绑定
     * @Date 10:18 2019/1/15
     * @Param
     **/
    @PostMapping(value = "/dup/list")
    public Object dupList(ExtensionDupDTO dupDTO, ExtensionPageData pageData, HttpServletRequest request) {

        try {
            logger.info("用户查询 DUP 信息");
            return Response.response(dupBindingService.selectAll(dupDTO,accessTokenService, request, pageData));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        }
    }

    /**
     * @return java.lang.Object
     * @Author Sjiang
     * @Description 根据 ID 获取已绑定信息
     * @Date 22:51 2019/1/22
     * @Param [dupDTO]
     **/

    @PostMapping(value = "/report/binding/{id}")
    public Object getBinding(ExtensionReportDTO reportDTO) {

        try {
            logger.info("根据 ID 获取绑定信息");
            return Response.response(reportService.getBindingById(reportDTO));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        }
    }

    /**
     * @return java.lang.Object
     * @Author Sjiang
     * @Description 根据 id 修改信息
     * @Date 23:27 2019/1/22
     * @Param [reportDTO]
     **/

    @PostMapping(value = "/report/update/{id}")
    public Object updateBinding(@RequestBody ExtensionReportDTO reportDTO, HttpServletRequest request) {

        try {

            logger.info("根据 ID 修改绑定信息");
            return Response.response(reportService.updateBinding(reportDTO, ExtensionReportUtils.getUserCode(accessTokenService, request)));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        }
    }

    /**
     * @return java.lang.Object
     * @Author Sjiang
     * @Description 根据 ID 撤销已绑定信息
     * @Date 10:42 2019/1/23
     * @Param [reportDTO]
     **/

    @PostMapping(value = "/report/remove/{id}")
    public Object removeBinding(ExtensionReportDTO reportDTO, HttpServletRequest request) {
        try {

            logger.info("根据 ID 撤销已绑定信息");
            return Response.response(reportService.removeBinding(reportDTO, ExtensionReportUtils.getUserCode(accessTokenService, request)));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        }
    }

    @PostMapping(value = "/upload", produces = "application/json;charset=utf-8")
    public Object uploadFile(@RequestParam(value = "uploadfile", defaultValue = "", required = true) MultipartFile multipartFile) {

        try {
            if (multipartFile == null || multipartFile.isEmpty()) {
                return Response.responseFail("请选择需要上传的文件");
            }
            logger.info("用户上传文件");
            return Response.response(ExtensionReportUtils.uploadFile(multipartFile, ossUtils));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        }
    }


    /**
     * @return java.lang.Object
     * @Author Sjiang
     * @Description 获取外拓项目所有信息的总条数
     * @Date 15:10 2019/1/23
     * @Param []
     **/

    @PostMapping(value = "/get/count")
    public Object getCount(HttpServletRequest request) {
        try {
            logger.info("获取外拓项目所有信息的总条数。");
            return Response.response(reportService.getExtensionCount(accessTokenService, request));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        }
    }

    /**
     * @return java.lang.Object
     * @Author Sjiang
     * @Description 用户删除已绑定数据的子数据信息
     * @Date 16:27 2019/2/12
     * @Param []
     **/
    @PostMapping(value = "/report/remove/child")
    public Object deleteBindingChildInfo(@RequestBody Map<String, Long> param, HttpServletRequest request) {
        try {

            logger.info("用户删除已绑定数据的子数据信息！");
            return Response.response(reportService.deleteBindingChildInfo(param, ExtensionReportUtils.getUserCode(accessTokenService, request)));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return Response.responseFail(e.getMessage());
        }
    }



}
