package com.sqx.modules.resumes.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.entity.UserBrowse;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserBrowseService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.firstLogin.entity.ResumesDto;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.myCollection.service.MyCollectionService;
import com.sqx.modules.resumes.entity.Resumes;
import com.sqx.modules.resumes.entity.ResumesDetails;
import com.sqx.modules.resumes.entity.ResumesListDto;
import com.sqx.modules.resumes.entity.RuleClassify;
import com.sqx.modules.resumes.service.ResumesDetailsService;
import com.sqx.modules.resumes.service.ResumesService;
import com.sqx.modules.resumes.service.RuleClassifyService;
import com.sqx.modules.resumes.service.ViewResumesService;
import com.sqx.modules.sendRecord.service.SendRecordService;
import com.sqx.modules.utils.pdf.CreatePDFMainTest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;

@RestController
@Api(value = "个人简历", tags = {"个人简历"})
@RequestMapping(value = "/app/resumes")
public class AppResumesController {

    @Autowired
    private ResumesService resumesService;
    @Autowired
    private ViewResumesService viewResumesService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private RuleClassifyService classifyService;
    @Autowired
    private SendRecordService sendRecordService;
    @Autowired
    private UserBrowseService userBrowseService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Login
    @PostMapping("/insertResumes")
    @ApiOperation("新增个人简历")
    public Result insertResumes(@RequestBody Resumes resumes, @RequestAttribute Long userId) {
        resumes.setUserId(userId);
        resumes.setStatus(1);
        resumes.setIsShow(1);
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setState("30");
        messageInfo.setIsSee("0");
        messageInfo.setContent(String.valueOf(resumes.getResumesEmail()));
        messageService.save(messageInfo);
        RuleClassify ruleClassify = classifyService.getById(resumes.getRuleClassifyId());
        RuleClassify twoClassify = classifyService.getOne(new QueryWrapper<RuleClassify>().eq("rule_classify_id", ruleClassify.getParentId()));
        RuleClassify oneClassify = classifyService.getOne(new QueryWrapper<RuleClassify>().eq("rule_classify_id", twoClassify.getParentId()));
        resumes.setRuleClassifyId(ruleClassify.getRuleClassifyId());
        resumes.setClassifyTwoId(twoClassify.getRuleClassifyId());
        resumes.setClassifyOneId(oneClassify.getRuleClassifyId());
        return resumesService.insertResumes(resumes);
    }

    @Login
    @PostMapping("/updateResumes")
    @ApiOperation("修改个人简历")
    public Result updateResumes(@RequestBody Resumes resumes) {
        return resumesService.updateResumes(resumes);
    }

    @Login
    @PostMapping("/updateResumess")
    @ApiOperation("修改个人简历不审核")
    public Result updateResumess(@RequestBody Resumes resumes) {
        return resumesService.updateResumess(resumes);
    }


    @Login
    @PostMapping("/deleteResumes")
    @ApiOperation("删除个人简历")
    public Result deleteResumes(Long resumesId) {
        userBrowseService.remove(new QueryWrapper<UserBrowse>().eq("resumes_id", resumesId));
        return resumesService.deleteResumesByResumesId(resumesId);
    }


    @GetMapping("/selectResumesByResumesId")
    @ApiOperation("查询简历详情")
    public Result selectResumesByResumesId(Long resumesId, Long userId, Long companyId) {
        return Result.success().put("data",resumesService.selectResumesByResumesId(resumesId, userId, companyId));
    }

    @GetMapping("/checkPower")
    @ApiOperation("检测获取简历信息")
    @Login
    public Result checkPower(Long resumesId,@RequestAttribute Long userId){
        return resumesService.checkPower(resumesId,userId);
    }

    @Login
    @GetMapping("/selectViewResumesList")
    @ApiOperation("查看自己购买过的简历")
    public Result selectViewResumesList(Integer page, Integer limit, @RequestAttribute Long userId) {
        return viewResumesService.selectResumesByUserId(page, limit, userId);
    }

    @Login
    @GetMapping("/selectResumesByUserId")
    @ApiOperation("查询自己的个人简历")
    public Result selectResumesByUserId(@RequestAttribute Long userId) {
        Resumes resumes = resumesService.selectResumesByUserId(userId);
        if (resumes != null) {
            return Result.success().put("data", resumesService.selectResumesByResumesId(resumes.getResumesId(), null, null));
        }
        return Result.success();
    }

    @Login
    @GetMapping("/selectResumesByUserIds")
    @ApiOperation("查询自己的个人简历")
    public Result selectResumesByUserIds(Long userId) {
        Resumes resumes = resumesService.selectResumesByUserId(userId);
        if (resumes != null) {
            return Result.success().put("data", resumesService.selectResumesByResumesId(resumes.getResumesId(), null, null));
        }
        return Result.success().put("data", resumes);
    }


    @PostMapping("/auditResumes")
    @ApiOperation("审核个人简历")
    public Result auditResumes(String ids, String content, Integer status) {
        return resumesService.auditResumes(ids, content, status);
    }


//    @GetMapping("/selectResumesList")
//    @ApiOperation("查询简历列表")
//    public Result selectResumesList(Integer page, Integer limit, Long companyId, Resumes resumes, Integer screen) {
//        return resumesService.selectResumesListByCompany(page, limit, companyId, resumes, screen);

    @PostMapping("selectResumesList")
    @ApiOperation("查询简历列表")
    public  Result selectResumesListHr(Integer page, Integer limit,@RequestBody ResumesListDto resumesDto){
        return  resumesService.selectResumesListHr(page,limit,resumesDto);
    }

    /**
     * 导出数据 生成pdf
     *
     * @param response
     */
    @ApiOperation(value = "导出数据 生成pdf")
    @GetMapping("/exportPdf")
    public void exportPdf(Long resumesId, HttpServletResponse response) {
        String http = commonInfoService.findOne(19).getValue();
        String[] split = http.split("://");
        Resumes resumes = resumesService.selectResumesByResumesIds(resumesId);
        // 模板路径
        String tempPath = "/www/wwwroot/"+split[1]+"/service/demo.pdf";

        //文字类
        Map<String, String> dataMap = new HashMap<>();
        // dataMap中的key要和模板中的域名对应
        dataMap.put("resumesName", resumes.getResumesName());
        dataMap.put("resumesAge", resumes.getResumesAge());
        dataMap.put("resumesPhone", resumes.getResumesPhone());
        dataMap.put("resumesEducation", resumes.getResumesEducation());
        dataMap.put("resumesMarriage", resumes.getResumesMarriage());
        dataMap.put("resumesCompensation", resumes.getResumesCompensation());
        dataMap.put("resumesEmail", resumes.getResumesEmail());
        dataMap.put("resumesWorkExperience", resumes.getResumesWorkExperience());
        dataMap.put("resumesIntention", resumes.getResumesIntention());
        dataMap.put("resumesMajor", resumes.getResumesMajor());
        dataMap.put("resumesPost", resumes.getResumesPost());
        dataMap.put("resumesDetails", resumes.getResumesDetails());
        dataMap.put("school", resumes.getSchool());
        Map<String, String> imgMap = new HashMap<>();
        // imgMap中的key要和模板中的域名对应
        imgMap.put("image1", "http://xiaohongmaoadmin.xianmxkj.com/demo.jpeg");
        imgMap.put("image2", "http://xiaohongmaoadmin.xianmxkj.com/demo.jpeg");
        imgMap.put("image3", "http://xiaohongmaoadmin.xianmxkj.com/demo.jpeg");
        imgMap.put("image4", "http://xiaohongmaoadmin.xianmxkj.com/demo.jpeg");
        imgMap.put("image5", "http://xiaohongmaoadmin.xianmxkj.com/demo.jpeg");
        imgMap.put("image6", "http://xiaohongmaoadmin.xianmxkj.com/demo.jpeg");
        List<List<String>> resumesCompanyList = new ArrayList<>();
        for (ResumesDetails resumesDetails : resumes.getResumesCompanyList()) {
            List<String> list2 = new ArrayList<>();
            list2.add(resumesDetails.getResumesTitle() + "       " + resumesDetails.getResumesPost() + "       " + resumesDetails.getResumesTime());
            resumesCompanyList.add(list2);
            list2 = new ArrayList<>();
            String resumesProject = resumesDetails.getResumesProject();
            list2.add(resumesProject);
            list2 = new ArrayList<>();
            String resumesContent = resumesDetails.getResumesContent();
            list2.add(resumesContent);
            resumesCompanyList.add(list2);
        }


        Map<String, List<List<String>>> listMap = new HashMap<>();
        // 这里的listMap中key要和模板中的域名对应
        listMap.put("resumesCompanyList", resumesCompanyList);

        Map<String, Object> o = new HashMap<>();
        o.put("tempPath", tempPath);
        o.put("dataMap", dataMap);
        o.put("imgMap", imgMap);
        o.put("tableList", listMap);
        try {
            FileOutputStream fos = new FileOutputStream("E:\\demo0001.pdf", true);
//            OutputStream out = response.getOutputStream();
//            response.setCharacterEncoding("utf-8");
//            response.setHeader("content-type", "application/octet-stream");

            String date = DateUtils.format(new Date());

            String fileName = URLEncoder.encode("pdf模板导出" + date + ".pdf", "UTF-8");
            //response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            try {
                CreatePDFMainTest.creatPdf(o, fos);
            } catch (Exception e) {
                e.printStackTrace();
            }
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Login
    @ApiOperation(value = "简历刷新")
    @PostMapping("refreshResumes")
    public Result refreshResumes(@RequestAttribute("userId") Long userId, Long resumesId) {
        return resumesService.refreshResumes(userId, resumesId);
    }

    @Login
    @ApiOperation(value = "修改求职状态")
    @PostMapping("updateResumesStatus")
    public Result updateResumesStatus(Long resumesId, Integer resumesStatus) {
        Resumes resumes = new Resumes();
        resumes.setResumesId(resumesId);
        resumes.setResumesStatus(resumesStatus);
        return resumesService.updateById(resumes) ? Result.success() : Result.error("修改失败,请重试!");
    }
    @Login
    @ApiOperation(value = "发送简历邮件")
    @PostMapping("sendResumes")
    public Result sendResumes(@RequestAttribute("userId")Long userId,Long to,Long postPushId){
        return resumesService.sendResumes(userId,to,postPushId);
    }
    @Login
    @ApiOperation(value = "简历置顶")
    @GetMapping("topResumes")
    public Result topResumes(@RequestAttribute("userId")Long userId){
        return sendRecordService.topResumes(userId);
    }


}
