package com.sqx.modules.firstLogin.control;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
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.firstLogin.entity.HrEntity;
import com.sqx.modules.firstLogin.service.HrService;
import com.sqx.modules.resumes.entity.Company;
import com.sqx.modules.resumes.entity.CompanyUser;
import com.sqx.modules.resumes.service.CompanyService;
import com.sqx.modules.resumes.service.CompanyUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Objects;

@RestController
@RequestMapping("/app/HrFirst" )
public class HrController {

@Autowired
private  HrService hrService;
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyUserService companyUserService;
    @Autowired
    private CompanyService companyService;

    @Login
    @PostMapping("addHr")
    @ApiOperation("hr设置名片信息")
    public Result addHr (@RequestAttribute Integer userId, @RequestBody HrEntity hr){
        if(hr.getHrImg()==null ||hr.getHrPosition() == null|| hr.getHrImg().isEmpty() || hr.getHrPosition().isEmpty()){
            return Result.error("请完整信息再次尝试");
        }
        hr.setUserId(userId);
        hrService.saveOrUpdate(hr);
        return  Result.success().put("data",hr);
    }
    @Login
    @GetMapping
    @ApiOperation("获取个人hr信息")
    public Result getHr(@RequestAttribute("userId")Long userId){
        return  Result.success().put("data",hrService.getByUserId(String.valueOf(userId)));
    }
    @Login
    @PostMapping("updateHr")
    @ApiOperation("个人中心hr信息")
    public Result updateHr(@RequestAttribute("userId")Long userId, @RequestBody HrEntity hr, UserEntity userEntity){
        userService.updateById(userEntity);
        hrService.updateById(hr);
        return Result.success("更新完成");
    }
    @Login
    @GetMapping("getUserHr")
    @ApiOperation("获取Hr信息")
    public Result getUserHr(@RequestAttribute("userId")Long userId){
        UserEntity userEntity= userService.selectUserById(userId);
        HrEntity hr = hrService.getByUserId(String.valueOf(userId));
        HashMap<String, Object> map = new HashMap<>();
        map.put("userEntity",userEntity);
        map.put("HrEntity",hr);
        CompanyUser companyUser = companyUserService.getOne(new QueryWrapper<CompanyUser>().eq("user_id",userId).eq("is_use",1));
        if (companyUser == null){
            QueryWrapper<Company> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq("user_id",userId).orderByDesc("update_time").last("limit 1 ");
            Company company = companyService.getBaseMapper().selectOne(queryWrapper);
            map.put("company",company);
        }else {
            Company company = companyService.getById(companyUser.getCompanyId());
            map.put("company",company);
        }
        return Result.success().put("data",map);
    }
}
