package com.sqx.modules.firstLogin.control;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
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.firstLogin.dto.WorkExpDto;
import com.sqx.modules.firstLogin.entity.*;
import com.sqx.modules.firstLogin.service.*;
import com.sqx.modules.intention.service.IntentionService;
import com.sqx.modules.resumes.entity.Resumes;
import com.sqx.modules.resumes.service.ResumesService;
import io.swagger.annotations.ApiOperation;
import jodd.util.CollectionUtil;
import org.apache.commons.collections.CollectionUtils;
import org.hibernate.jdbc.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;


@RestController
@RequestMapping("/app/userFirst" )
public class UserFirstRegistController {
    @Autowired
    private ResumesService resumesService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private WorkExpService workExpService;
    @Autowired
    private PostSkillService postSkillService;
    @Autowired
    private EduService eduService;
    @Autowired
    private UserService userService;
    @Autowired
    private SkillService skillService;
    @Autowired
    private IntentionService intentionService;
    @Autowired
    private WorkExpDetailService workExpDetailService;
    @Autowired
    private UserBrowseService userBrowseService;


    @Login
    @PostMapping ("regist")
    @ApiOperation("初次加载信息")
    public Result userFirstRegist(@RequestAttribute("userId") Long userId,@RequestBody Resumes resumes) {
        Resumes oldResumes = resumesService.selectResumesByUserId(userId);
        if (oldResumes != null) {
            QueryWrapper<Resumes> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("resumes_id",oldResumes.getResumesId());
            resumes.setStatus(0);
            resumes.setUpdateTime(String.valueOf(LocalDateTime.now()));
            resumesService.update(resumes,queryWrapper);
        }else{
            resumes.setUserId(userId);
            resumes.setStatus(0);
            resumesService.save(resumes);
        }
        return  Result.success();
    }
    @Login
    @GetMapping ("getUserResumes")
    @ApiOperation("获取简历信息")
    public Result getUserResumes(@RequestAttribute Long userId) {
        ResumesDto resumesDto = new ResumesDto();
        resumesDto.setUserEntity(userService.getById(userId));
        resumesDto.setResumeList( resumesService.selectResumesByUserId(userId));
        resumesDto.setEduList(eduService.selectEdu(Math.toIntExact(userId)));
        resumesDto.setIntentions(intentionService.getIntention(userId));
        resumesDto.setWorkExps(workExpService.selectWorkExpOnly(userId));
        resumesDto.setWorkExpList(workExpService.selectWorkExp(Math.toIntExact(userId)));
        resumesDto.setSkills(skillService.selectSkill(Math.toIntExact(userId)));
        resumesDto.setWorkExpTimes(workExpService.getWorkTime(userId));
        return Result.success().put("data",resumesDto);
    }
    @Login
    @GetMapping
    @ApiOperation("获取简历详情")
    public Result getResumesByResumesId(@RequestAttribute("userId")Long userId, Long resumesId){
        ResumesDto resumesDto =new ResumesDto();
        Resumes resumes = resumesService.getById(resumesId);
        String userId1 = String.valueOf(resumes.getUserId());
        resumesDto.setUserEntity(userService.getById(userId1));
        resumesDto.setResumeList( resumes);
        resumesDto.setEduList(eduService.selectEdu(Integer.valueOf(userId1)));
        resumesDto.setIntentions(intentionService.getIntention(Long.valueOf(userId1)));
        resumesDto.setWorkExps(workExpService.selectWorkExpOnly(Long.valueOf(userId1)));
        resumesDto.setWorkExpList(workExpService.selectWorkExp(Integer.valueOf(userId1)));
        resumesDto.setSkills(skillService.selectSkill(Integer.valueOf(userId1)));
        resumesDto.setWorkExpTimes(workExpService.getWorkTime(Long.valueOf(userId1)));
        UserBrowse userBrowse =new UserBrowse();
        userBrowse.setUserId(userId);
        userBrowse.setResumesId(resumesId);
        userBrowse.setBrowseType(2);
        userBrowse.setUpdateTime(LocalDateTime.now());
        userBrowseService.save(userBrowse);
        return Result.success().put("data",resumesDto);
    }
    @Login
    @GetMapping ("getWorkExpDetailOne")
    @ApiOperation("获取简历信息")
    public Result getWorkExpDetailOne(Long workExpId) {
        return Result.success().put("data",workExpService.getWorkExpDetailOne(workExpId));
    }
    @Login
    @PostMapping("savemsg")
    @ApiOperation("更改基础信息")
    public  Result savemsg(@RequestAttribute("userId")Long userId, @RequestBody UserEntity userEntity,@RequestBody Resumes resumes){
        userEntity.setUserId(userId);
        userEntity.setUpdateTime(String.valueOf(LocalDateTime.now()));
        Resumes oldresumes =resumesService.selectResumesByUserId(userId);
        resumes.setUserId(oldresumes.getUserId());
        resumes.setUpdateTime(String.valueOf(LocalDateTime.now()));
        resumesService.updateById(resumes);
        return  Result.success();
    }


    @PostMapping("getDepartment")
    @ApiOperation("获取工作部门列表")
    public Result getDepartment(Department department) {
        return Result.success().put("data", departmentService.getDepartmentList(department));
    }

    @GetMapping("getPostSkill")
    @ApiOperation("获取专业技能")
    public Result getPostSkill( PostSkill postSkill) {
        return Result.success().put("data", postSkillService.getPostSkill(postSkill));
    }

    @Login
    @PostMapping("addWorkExp")
    @ApiOperation("新增或修改工作经验")
    public  Result addWorkExp(@RequestAttribute("userId") Long userId, @RequestBody WorkExpDto workExp){
        WorkExp workExp1 = workExp.getWorkExp();
        // 存入数据库
        workExp1.setUserId(userId);
        workExp1.setIsUse(1);
        workExpService.saveOrUpdate(workExp1);
        List<WorkExpDetail> workExpDetails = workExp.getWorkExpDetails();
        workExpDetails.forEach(workExpDetail -> workExpDetail.setWorkExpId(workExp1.getWorkExpId()));
        workExpDetails.forEach(workExpDetail ->workExpDetailService.saveOrUpdate(workExpDetail));
        Resumes resumes = resumesService.getOne(new QueryWrapper<Resumes>().eq("user_id",userId));
        int times = workExpService.getWorkTime(userId);
        resumes.setResumesWorkExperience(String.valueOf(times));
        return Result.success();
    }
    @Login
    @PostMapping("addEdu")
    @ApiOperation("增加或修改学历")
    public Result addEdu(@RequestAttribute("userId") Long userId,@RequestBody  Edu edu) {
        edu.setUserId(userId);
        edu.setIsUse(1);
        eduService.saveOrUpdate(edu);
        Edu edu1 =eduService.getBaseMapper().selectOne(new QueryWrapper<Edu>().eq("userId",userId).orderByDesc("end_time").last("1"));
        Resumes resumes = resumesService.getOne(new QueryWrapper<Resumes>().eq("user_id",userId));
        resumes.setResumesEducation(edu1.getDegree());
        return Result.success();
    }
    @Login
    @PostMapping("addSkill")
    @ApiOperation("增加技能")
    public Result addSkill(@RequestAttribute("userId") Long userId,@RequestBody List<Skill> skills) {
        skills.forEach(skill -> skill.setUserId(userId));
        // 设置isUse为1
        skills.forEach(skill -> skill.setIsUse(1));
        // 批量保存或更新
        skillService.saveOrUpdateBatch(skills);
        return Result.success();
    }
    @Login
    @GetMapping("selectEdu")
    @ApiOperation("查找学历")
    public Result selectEdu(Long eduId) {
        return Result.success().put("data", eduService.getById(eduId));
    }
    @Login
    @GetMapping("selectWorkExp")
    @ApiOperation("查找工作经历")
    public Result selectWorkExp(Integer workExpId) {
        return Result.success().put("data", workExpService.selectWorkExpOne(workExpId));
    }
    @Login
    @GetMapping("selectSkill")
    @ApiOperation("查找技能")
    public Result selectSkill(@RequestAttribute("userId") Integer userId) {
        return Result.success().put("data", skillService.selectSkill(userId));
    }
    @Login
    @PostMapping("deleteWorkExp")
    @ApiOperation("删除工作经历")
    public Result deleteExp(@RequestBody WorkExp workExp) {
        workExp.setIsUse(0);
        workExpService.saveOrUpdate(workExp);
        return Result.success();
    }
    @Login
    @PostMapping("deleteEdu")
    @ApiOperation("删除教育经历")
    public Result deleteEdu( @RequestBody Edu edu) {
        edu.setIsUse(0);
        eduService.saveOrUpdate(edu);
        return Result.success();
    }
    @Login
    @PostMapping("deleteSkill")
    @ApiOperation("删除技能")
    public Result deleteSkill(@RequestBody List<Skill> skills) {
        skills.forEach(skill -> skill.setIsUse(0));
        skillService.updateBatchById(skills);
        return Result.success();
    }
    @Login
    @PostMapping("updateSkill")
    @ApiOperation("更新技能")
    public Result deleteSkill(@RequestBody List<Skill> skills,@RequestAttribute("userId")Integer userId) {
        List<Skill> oldSkills = skillService.selectSkill(userId);
        if (CollectionUtils.isNotEmpty(oldSkills)){
        oldSkills.forEach(skill -> skill.setIsUse(0));
        skillService.updateBatchById(oldSkills);
        }
        if (CollectionUtils.isNotEmpty(skills)){
        skills.forEach(skill -> skill.setUserId(Long.valueOf(userId)));
        skills.forEach(skill -> skill.setIsUse(1));
        skillService.saveBatch(skills);
        }
        return Result.success();

    }
}
