package com.sqx.modules.resumes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.resumes.entity.AuditResumes;
import com.sqx.modules.resumes.entity.PostPush;
import com.sqx.modules.resumes.entity.Resumes;
import com.sqx.modules.resumes.service.*;
import com.sqx.modules.utils.SenInfoCheckUtil;
import com.sqx.modules.utils.excel.ExcelData;
import com.sqx.modules.utils.excel.ExportExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

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

    @Autowired
    private ResumesService resumesService;
    @Autowired
    private ResumesDetailsService resumesDetailsService;
    @Autowired
    private ViewResumesService viewResumesService;
    @Autowired
    private AuditResumesService auditResumesService;
    @Autowired
    private PostPushService postPushService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserService userService;

    @PostMapping("/insertResumes")
    @ApiOperation("新增个人简历")
    public Result insertResumes(@RequestBody Resumes resumes){
        resumes.setStatus(2);
        resumes.setIsShow(1);
        return resumesService.insertResumes(resumes);
    }
    @GetMapping("/updateShow")
    @ApiOperation("修改简历上下架")
    public Result updateShow(Long resumesId){
        return resumesService.updateShow(resumesId);
    }

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

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

    @PostMapping("/deleteResumes")
    @ApiOperation("删除个人简历")
    public Result deleteResumes(Long resumesId){
        return resumesService.deleteResumesByResumesId(resumesId);
    }

    @PostMapping("/updateResumesIsRecommend")
    @ApiOperation("修改是否推荐")
    public Result updateResumesIsRecommend(Long resumesId){
        Resumes resumes = resumesService.getById(resumesId);
        UserEntity userEntity = userService.selectUserById(resumes.getUserId());
        if(resumes.getIsRecommend()==null || resumes.getIsRecommend()==1){
            resumes.setIsRecommend(2);
            String value = commonInfoService.findOne(310).getValue();
            if(userEntity!=null){
                List<String> msgList=new ArrayList<>();
                msgList.add("简历通知");
                msgList.add("您的简历已经变更为推荐");
                msgList.add(DateUtils.format(new Date()));
                SenInfoCheckUtil.sendMsg(userEntity.getOpenId(),value,msgList,3,null);
            }
        }else{
            resumes.setIsRecommend(1);
        }
        if(userEntity!=null){
            userService.updateById(userEntity);
        }
        resumesService.updateById(resumes);

        return Result.success();
    }

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

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

    @PostMapping("/auditResumes")
    @ApiOperation("审核个人简历")
    public Result auditResumes(String ids,String content,Integer status){
        return resumesService.auditResumes(ids,content,status);
    }
    @GetMapping("/exportExcelResumesList")
    @ApiOperation("导出简历列表")
    public void exportExcelResumesList(String startTime,String endTime,Resumes resumes, HttpServletResponse response) throws Exception {
        ExcelData excelData = resumesService.exportExcelResumesList(startTime,endTime,resumes);
        ExportExcelUtils.exportExcel(response, "简历列表.xlsx", excelData);
    }


    @GetMapping("/selectResumesList")
    @ApiOperation("查询简历列表")
    public Result selectResumesList(Integer page,Integer limit,String startTime,String endTime,Resumes resumes){
        return resumesService.selectResumesList(page, limit,startTime,endTime, resumes);
    }



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

    @GetMapping("/selectAuditResumesList")
    @ApiOperation("查询简历审核记录")
    public Result selectAuditResumesList(Integer page,Integer limit,Long resumesId){
        IPage<AuditResumes> resumesPage = auditResumesService.page(new Page<>(page, limit), new QueryWrapper<AuditResumes>().eq("resumes_id", resumesId).orderByDesc("create_time"));
        return Result.success().put("data",new PageUtils(resumesPage));
    }

    @GetMapping("/selectDataStatistics")
    @ApiOperation("数据统计")
    public Result selectDataStatistics(String time,Integer flag){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = new Date();
        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(flag==1){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            time=simpleDateFormat.format(parse);
        }else if(flag==2){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM");
            time=simpleDateFormat.format(parse);
        }else{
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy");
            time=simpleDateFormat.format(parse);
        }
        //待入职简历  已入职简历  待审核简历  总简历数量
        int awaitResumesCount = resumesService.count(new QueryWrapper<Resumes>().eq("status", 2).like("update_time",time));
        int entryResumesCount = resumesService.count(new QueryWrapper<Resumes>().eq("status", 4).like("update_time",time));
        int auditResumesCount = resumesService.count(new QueryWrapper<Resumes>().eq("status", 1).like("update_time",time));
        int resumesCount = resumesService.count(new QueryWrapper<Resumes>().like("update_time",time));
        //待推送项目  已推送项目  待审核项目  总项目数量
        int awaitPostPushCount = postPushService.count(new QueryWrapper<PostPush>().eq("status", 2).like("update_time",time));
        int entryPostPushCount = postPushService.count(new QueryWrapper<PostPush>().eq("status", 3).like("update_time",time));
        int auditPostPushCount = postPushService.count(new QueryWrapper<PostPush>().eq("status", 1).like("update_time",time));
        int postPushCount = postPushService.count(new QueryWrapper<PostPush>().like("update_time",time));
        Map<String,Object> result=new HashMap<>();
        result.put("awaitResumesCount",awaitResumesCount);result.put("entryResumesCount",entryResumesCount);
        result.put("auditResumesCount",auditResumesCount);result.put("resumesCount",resumesCount);
        result.put("awaitPostPushCount",awaitPostPushCount);result.put("entryPostPushCount",entryPostPushCount);
        result.put("auditPostPushCount",auditPostPushCount);result.put("postPushCount",postPushCount);
        return Result.success().put("data",result);
    }
}
