package cn.timer.api.controller.zpgl;

import cn.timer.api.aspect.lang.annotation.Log;
import cn.timer.api.aspect.lang.enums.BusinessType;
import cn.timer.api.bean.qyzx.QyzxEntInfoM;
import cn.timer.api.bean.zpgl.*;
import cn.timer.api.config.enuminterface.JxglEnumInterface;
import cn.timer.api.controller.zpgl.sevice.ZpglMslcLogService;
import cn.timer.api.dto.zpgl.InterviewApplicationDto;
import cn.timer.api.utils.DateFormatUtils;
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
import cn.timer.api.utils.ZpglMessageTemplate;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "招聘管理")
@Transactional(rollbackOn = Exception.class)
@RestController
@RequestMapping(value = "/zpgl/h5")
public class ZpglH5Controller {



    @Autowired
    private ZpglMslcLogService zpglMslcLogService;

    /**
     * 添加面试申请
     * @param
     * @return
     */
    @RequestMapping("/add_interview_application")
    @ApiOperation(value = "添加面试申请", httpMethod = "POST", notes = "添加面试申请")
    @Log(title = "addInterviewApplication", businessType = BusinessType.INSERT_UPDATE)
    public Result<Object> addInterviewApplication(@Validated @RequestBody InterviewApplicationDto dto) {

        System.out.println(dto);
        ZpglRcxx zpglRcxx = new ZpglRcxx();
        //复制到 ZpglRcxx 实体对象
        BeanUtils.copyProperties(dto,zpglRcxx);
        /**
         * 新增需要添加
         * 是否初始化面试流程记录
         */
        boolean insertZpglMslcLogFlag = StringUtils.isEmpty(zpglRcxx.getId());
        // 计算工作年限
        if(!StringUtils.isEmpty(zpglRcxx.getWorkBeginYear())){
            zpglRcxx.setWorkyears(DateFormatUtils.yearCompare(zpglRcxx.getWorkBeginYear(),new Date()));
        }
        zpglRcxx.insertOrUpdate();

        if(insertZpglMslcLogFlag){
            // 新增人才信息 初始化流程记录
            ZpglMslcLog zpglMslcLog = new ZpglMslcLog();
            zpglMslcLog.setZpglRcxxId(zpglRcxx.getId());
            zpglMslcLog.setUserId(0);
            zpglMslcLog.setUserName("");
            zpglMslcLog.setZpglRcxxStatus(JxglEnumInterface.MslcLogStatus.OTHER.getType());
            zpglMslcLog.setDetail(ZpglMessageTemplate.SYS_ADD);
            zpglMslcLogService.addZpglMslcLog(zpglMslcLog);
        }

        //添加紧急联系人
        if(dto.getZpglRcxxLxxxb() != null){
            ZpglRcxxLxxxb zpglRcxxLxxxb = dto.getZpglRcxxLxxxb();
            zpglRcxxLxxxb.setZpglRcxxId(zpglRcxx.getId());
            zpglRcxxLxxxb.insertOrUpdate();
        }

        //新增/修改工作经验
        if(dto.getZpglRcxxExperience() !=null && dto.getZpglRcxxExperience().size()>0){
            List<ZpglRcxxExperience> experiences = dto.getZpglRcxxExperience();
            for(ZpglRcxxExperience experience : experiences){
                if(experience.getCompany() != null){
                    experience.setZpglRcxxId(zpglRcxx.getId());
                    experience.insertOrUpdate();
                }
            }
        }

        //新增/修改教育经历
        if(dto.getZpglRcxxStudy() !=null && dto.getZpglRcxxStudy().size()>0){
            List<ZpglRcxxStudy> zpglRcxxStudys = dto.getZpglRcxxStudy();
            for(ZpglRcxxStudy study : zpglRcxxStudys){
                if(study.getSchoolName() != null){
                    study.setZpglRcxxId(zpglRcxx.getId());
                    study.insertOrUpdate();
                }
            }
        }
        //新增/修改家庭资料
        if(dto.getZpglRcxxJtzls() !=null && dto.getZpglRcxxJtzls().size()>0){
            List<ZpglRcxxJtzl> zpglRcxxJtzls = dto.getZpglRcxxJtzls();
            for(ZpglRcxxJtzl jtzl : zpglRcxxJtzls){
                if(jtzl.getName() != null){
                    jtzl.setZpglRcxxId(zpglRcxx.getId());
                    jtzl.insertOrUpdate();
                }
            }
        }


        return ResultUtil.success();
    }

    /**
     * 查询面试申请信息
     * @param
     * @return
     */
    @RequestMapping("/interview_application_info")
    @ApiOperation(value = "查询面试申请信息", httpMethod = "GET", notes = "查询面试申请信息")
    @Log(title = "interviewApplicationIofo", businessType = BusinessType.INSERT_UPDATE)
    public Result<InterviewApplicationDto> interviewApplicationIofo(@RequestParam String mobile,Integer zpglRcxxId) {

        InterviewApplicationDto dto = new InterviewApplicationDto();
        List<ZpglRcxx> zpglRcxxes = new ArrayList<>();
        if(null != zpglRcxxId){
            zpglRcxxes = ZpglRcxx.builder().build()
                    .selectList(new QueryWrapper<ZpglRcxx>().lambda()
                            .eq(ZpglRcxx::getId, zpglRcxxId));
        }else{
            zpglRcxxes = ZpglRcxx.builder().build()
                    .selectList(new QueryWrapper<ZpglRcxx>().lambda()
                            .eq(ZpglRcxx::getMobile, mobile));
        }
        if(zpglRcxxes.size() == 0){
            return ResultUtil.error("没有简历信息");
        }



        ZpglRcxx zpglRcxx = zpglRcxxes.get(0);
        //复制到dto
        BeanUtils.copyProperties(zpglRcxx,dto);

        //联系人信息
        List<ZpglRcxxLxxxb> zpglRcxxLxxxbs = ZpglRcxxLxxxb.builder().build().selectList(new QueryWrapper<ZpglRcxxLxxxb>()
                .lambda().select(ZpglRcxxLxxxb::getId,ZpglRcxxLxxxb::getLinkName,ZpglRcxxLxxxb::getLinkMobile)
                .eq(ZpglRcxxLxxxb::getZpglRcxxId, zpglRcxx.getId()));
        if(zpglRcxxLxxxbs.size()>0)
            dto.setZpglRcxxLxxxb(zpglRcxxLxxxbs.get(0));



        //工作经验
        List<ZpglRcxxExperience> experiences = ZpglRcxxExperience.builder().build()
                .selectList(new QueryWrapper<ZpglRcxxExperience>()
                .lambda().eq(ZpglRcxxExperience::getZpglRcxxId, zpglRcxx.getId()));
        if (experiences.size()>0)
            dto.setZpglRcxxExperience(experiences);

        //教育经历
        List<ZpglRcxxStudy> study = ZpglRcxxStudy.builder().build()
                .selectList(new QueryWrapper<ZpglRcxxStudy>()
                        .lambda().eq(ZpglRcxxStudy::getZpglRcxxId, zpglRcxx.getId()));
        if (study.size()>0)
            dto.setZpglRcxxStudy(study);

        //家庭资料
        List<ZpglRcxxJtzl> jtzl = ZpglRcxxJtzl.builder().build()
                .selectList(new QueryWrapper<ZpglRcxxJtzl>()
                        .lambda().eq(ZpglRcxxJtzl::getZpglRcxxId, zpglRcxx.getId()));
        if (jtzl.size()>0)
            dto.setZpglRcxxJtzls(jtzl);

        return ResultUtil.data(dto,"查询成功");
    }

    @RequestMapping("/getOrgName")
    @ApiOperation(value = "根据orgCode获取企业名称", httpMethod = "GET", notes = "获取企业名称")
    public Object getOrgName(@RequestParam("orgCode") Integer orgCode){
        QyzxEntInfoM qyzxEntInfoM = QyzxEntInfoM.builder().build()
                .selectOne(new QueryWrapper<QyzxEntInfoM>().lambda()
                        .select(QyzxEntInfoM::getName).eq(QyzxEntInfoM::getId, orgCode));

        return ResultUtil.data(qyzxEntInfoM);
    }

}