package com.ruoyi.cms.controller.cms;

import com.alibaba.excel.util.StringUtils;
import com.ruoyi.cms.domain.Job;
import com.ruoyi.cms.domain.JobApply;
import com.ruoyi.cms.domain.vo.CandidateVO;
import com.ruoyi.cms.service.IAppUserService;
import com.ruoyi.cms.service.IJobApplyService;
import com.ruoyi.cms.util.RoleUtils;
import com.ruoyi.cms.util.StringUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.entity.Company;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/cms/jobApply")
@Api(tags = "后台：岗位申请")
public class JobApplyController extends BaseController {

    @Autowired
    IJobApplyService iJobApplyService;
    @Autowired
    private IAppUserService appUserService;

    @GetMapping("/trendChart")
    public AjaxResult trendChart(JobApply jobApply)
    {
        HashMap<String,Integer> result = iJobApplyService.trendChart(jobApply);
        return success(result);
    }

    /**
     * 导出APP用户列表
     */
    @ApiOperation("导出岗位申请APP用户")
    @PreAuthorize("@ss.hasPermi('cms:jobApply:export')")
    @Log(title = "APP用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Job job)
    {
        List<CandidateVO> list = iJobApplyService.selectAppUserList(job);
        ExcelUtil<CandidateVO> util = new ExcelUtil<CandidateVO>(CandidateVO.class);
        util.exportExcel(response, list, "APP用户数据");
    }

    @ApiOperation("获取求职者列表")
    @PreAuthorize("@ss.hasPermi('cms:jobApply:applyJobUserList')")
    @GetMapping("/applyJobUserList")
    public TableDataInfo applyJobList(AppUser appUser)
    {
        if (RoleUtils.isCompanyAdmin()) {
            Company company=new Company();
            company.setCode(RoleUtils.getCurrentUseridCard());
        }
        startPage();
        List<CandidateVO> list = iJobApplyService.selectApplyJobUserList(appUser);
        return getDataTable(list);
    }

    @PutMapping
    @Log(title = "岗位", businessType = BusinessType.UPDATE)
    @ApiOperation("用户申请岗位")
    public AjaxResult apply(@RequestBody JobApply jobApply)
    {
        if(jobApply.getJobId()==null){
            return AjaxResult.error("岗位id为空");
        }
        if(!SecurityUtils.isLogin()){
            return AjaxResult.error("用户未登录!");
        }
        if(jobApply.getUserId()==null){
            String idCard=RoleUtils.getCurrentUseridCard();
            AppUser appUser=appUserService.selectAppuserByIdcard(idCard);
            if(appUser==null){
                return AjaxResult.error("用户信息未完善,请完善身份证信息!");
            }else{
                jobApply.setUserId(appUser.getUserId());
            }
        }
        return success(iJobApplyService.applyComJob(jobApply));
    }

    @Log(title = "岗位", businessType = BusinessType.UPDATE)
    @ApiOperation("求职者管理-用户列表录用")
    @PutMapping("/applyAgree")
    public AjaxResult applyAgree(@RequestBody JobApply jobApply)
    {
        if(jobApply.getId()==null){
            return AjaxResult.error("参数id为空");
        }
        jobApply.setHire(StringUtil.HIRE_LY);
        jobApply.setHireSource(StringUtil.HIRE_SOURCE_SYSTEM);
        return success(iJobApplyService.updateJobApply(jobApply));
    }

    @Log(title = "岗位", businessType = BusinessType.INSERT)
    @ApiOperation("招聘会-岗位申请")
    @PostMapping("/zphApply")
    public AjaxResult zphApply(@RequestBody JobApply jobApply)
    {
        if(jobApply.getJobId()==null){
            return AjaxResult.error("岗位id为空");
        }
        String idCard = jobApply.getIdCard();
        if(StringUtils.isBlank(idCard)){
            return AjaxResult.error("身份证为空!");
        }
        if (!idCard.matches(StringUtil.SFZ_VALID_REGEX)) {
            return AjaxResult.error("身份证格式不正确");
        }
        AppUser appUser=appUserService.selectAppuserByIdcard(idCard);
        if(appUser==null){
            return AjaxResult.error("在系统库中未查询到当前用户信息!");
        }
        jobApply.setUserId(appUser.getUserId());
        JobApply parm=new JobApply();
        parm.setJobId(jobApply.getJobId());
        parm.setUserId(appUser.getUserId());
        List<JobApply> applies=iJobApplyService.selectJobApplyList(parm);
        if(applies!=null&&applies.size()>0){
            return AjaxResult.error("当前用户，已申请过该岗位，请勿重复申请!");
        }
        return success(iJobApplyService.applyComJob(jobApply));
    }

    @Log(title = "岗位", businessType = BusinessType.INSERT)
    @ApiOperation("招聘会-岗位录用")
    @PostMapping("/zphApplyAgree")
    public AjaxResult zphApplyAgree(@RequestBody JobApply jobApply)
    {
        if(jobApply.getJobId()==null){
            return AjaxResult.error("岗位id为空");
        }
        String idCard = jobApply.getIdCard();
        if(StringUtils.isBlank(idCard)){
            return AjaxResult.error("身份证为空!");
        }
        if (!idCard.matches(StringUtil.SFZ_VALID_REGEX)) {
            return AjaxResult.error("身份证格式不正确");
        }
        AppUser appUser=appUserService.selectAppuserByIdcard(idCard);
        if(appUser==null){
            return AjaxResult.error("在系统库中未查询到当前用户信息!");
        }
        jobApply.setUserId(appUser.getUserId());
        jobApply.setHire(StringUtil.HIRE_LY);
        jobApply.setHireSource(StringUtil.HIRE_SOURCE_ZPH);
        return success(iJobApplyService.updateJobZphApply(jobApply));
    }
}
