package com.bonus.gs.sub.evaluate.evaluate.controller;

import com.bonus.gs.sub.evaluate.evaluate.beans.EvalSummaryBean;
import com.bonus.gs.sub.evaluate.evaluate.beans.EvaluateBean;
import com.bonus.gs.sub.evaluate.evaluate.beans.OutSourceBean;
import com.bonus.gs.sub.evaluate.evaluate.service.EvalSummaryService;
import com.bonus.gs.sub.evaluate.manager.controller.BaseController;
import com.bonus.gs.sub.evaluate.manager.utils.AjaxRes;
import com.bonus.gs.sub.evaluate.manager.utils.GlobalConst;
import com.bonus.gs.sub.evaluate.manager.utils.UserUtil;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.util.List;

@Controller
@RequestMapping("/backstage/evaluate/")
public class EvalSummaryController extends BaseController<EvaluateBean> {
    @Autowired
    private EvalSummaryService service;

    @Value("${upload.dir}")
    private String uploadDir;

    /**
     * 新增评价汇总
     *
     * @param evaluateTitle 评价标题
     * @param date          评价时间
     * @param file          文件
     * @return AjaxRes
     */
    @RequestMapping(path = "addEvalSummary", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes addEvalSummary(@RequestParam("evaluateTitle") String evaluateTitle,
                                  @RequestParam("date") String date,
                                  @RequestParam("evaluateDept") String evaluateDept,
                                  @RequestParam("file") MultipartFile file) {
        AjaxRes ar = getAjaxRes();
        if (file != null && !file.isEmpty()) {
            // 验证文件类型
            String originalFileName = file.getOriginalFilename();
            String fileExtension = originalFileName != null ? originalFileName.split("\\.")[1] : "";
            if (!"xls".equalsIgnoreCase(fileExtension) && !"xlsx".equalsIgnoreCase(fileExtension)) {
                ar.setFailMsg("文件类型错误，请上传xls或xlsx格式的文件");
                return ar;
            }
            String fileName = file.getOriginalFilename();
        }
        try {
            File targetDir = new File(uploadDir);
            if (!targetDir.exists()) {
                targetDir.mkdirs();
            }
            String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
            File targetFile = new File(uploadDir, fileName);
            file.transferTo(targetFile);
            // 返回文件路径
            String pathname = targetFile.getAbsolutePath();
            ar.setSucceedMsg("文件上传成功");
            int result = service.addEvalSummary(pathname, date, evaluateTitle,evaluateDept);
            ar.setSucceed(result);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            ar.setFailMsg("文件上传失败");
        }
        return ar;
    }

    /**
     * 线下评价汇总列表
     * @param bean 查询条件
     * @return AjaxRes
     */
    @RequestMapping(path = "evalSummaryList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxRes evalSummaryList(EvalSummaryBean bean) {
        AjaxRes ar = getAjaxRes();
        try {
            List<OutSourceBean> list = service.getEvalSummaryList(bean);
            ar.setListSucceed(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    /**
     * 专员汇总列表
     * @param bean 查询条件
     * @return AjaxRes
     */
    @RequestMapping(path = "dedicatedEvalSummaryList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxRes dedicatedEvalSummaryList(EvalSummaryBean bean) {
        AjaxRes ar = getAjaxRes();
        try {
            List<OutSourceBean> list = service.dedicatedEvalSummaryList(bean);
            ar.setListSucceed(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }
    /**
     * 线下评价汇总列表
     * @param bean 查询条件
     * @return AjaxRes
     */
    @RequestMapping(path = "offlineSummaryList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxRes offlineSummaryList(EvalSummaryBean bean) {
        AjaxRes ar = getAjaxRes();
        try {
            List<OutSourceBean> list = service.getOfflineSummaryList(bean);
            ar.setListSucceed(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }


    @RequestMapping("deleteOffEvaluateById")
    @ResponseBody
    public AjaxRes deleteOffEvaluateById(String id) {
        AjaxRes ar = new AjaxRes();
        try {
            int result = service.deleteOffEvaluateById(id);
            if(result > 0) {
                ar.setRes(GlobalConst.SUCCEED);
                ar.setResMsg("删除成功");
            }else {
                ar.setFailMsg(GlobalConst.DATA_FAIL);
                ar.setResMsg("删除失败,请联系系统管理员");
            }

        }catch(Exception e) {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }
}
