package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.ContractService;
import org.jeecg.modules.employAhthenticationCompany.entity.EmployAhthenticationCompany;
import org.jeecg.modules.employAhthenticationCompany.service.IEmployAhthenticationCompanyService;
import org.jeecg.modules.employAuthenticationPerson.entity.EmployAuthenticationPerson;
import org.jeecg.modules.employAuthenticationPerson.service.IEmployAuthenticationPersonService;
import org.jeecg.modules.employContract.entity.EmployContract;
import org.jeecg.modules.employContract.service.IEmployContractService;
import org.jeecg.modules.employResume.entity.EmployResume;
import org.jeecg.modules.employResume.service.IEmployResumeService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ContractServiceImpl implements ContractService {

    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;


    //电子合同表
    @Resource
    private IEmployContractService employContractService;

    //企业实名认证表
    @Resource
    private IEmployAhthenticationCompanyService ahthenticationCompanyService;

    //个人实名认证表
    @Resource
    private IEmployAuthenticationPersonService authenticationPersonService;

    //简历信息表
    @Resource
    private IEmployResumeService employResumeService;
    /******************************************************************************************************************/

    //电子合同-获取电子合同列表
    @Override
    public Result<?> queryContractList(String token, String role, EmployContract employContract, PageBean pageBean) {
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            //分页
            Page<EmployContract> page = new Page<EmployContract>(pageBean.getPageNo(), pageBean.getPageSize());
            LambdaQueryChainWrapper<EmployContract> query = employContractService
                    .lambdaQuery()
                    .eq(EmployContract::getBossId, hanHaiMember.getId());

            //组装查询条件
            if("0".equals(role)){
                //用户为个人
                query.eq(EmployContract::getPersonId, hanHaiMember.getId());
            }else if("1".equals(role)){
                //用户为企业
                query.eq(EmployContract::getBossId, hanHaiMember.getId());
            }else {
                return Result.error("角色类型输入错误，电子合同列表查询失败");
            }
            if(null != employContract.getSignCompany()){
                query.eq(EmployContract::getSignCompany, employContract.getSignCompany());
            }
            if(null != employContract.getSignPerson()){
                query.eq(EmployContract::getSignPerson, employContract.getSignPerson());
            }

            //获取电子合同信息
            Page<EmployContract> pageList = query.page(page);

            return Result.OK("电子合同列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("电子合同列表查询失败");
        }

    }

    //电子合同-获取电子合同详情
    @Override
    public Result<?> queryContracById(String token, String contractId) {
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            EmployContract one = employContractService
                    .lambdaQuery()
                    .eq(EmployContract::getId, contractId)
                    .one();

            return Result.OK("电子合同详情", one);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("电子合同详情查询失败");
        }
    }

    //添加电子合同信息
    public Result<?> addContract(String token, String resumeId) {
        String message = "电子合同添加失败！";

        //电子合同对象
        EmployContract contract = new EmployContract();

        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            //获取招聘者id
            contract.setBossId(hanHaiMember.getId());

            //获取招聘者实名公司名称
            EmployAhthenticationCompany company = ahthenticationCompanyService
                    .lambdaQuery()
                    .eq(EmployAhthenticationCompany::getUserId, hanHaiMember.getId())
                    .one();
            contract.setNameA(company.getCompanyName());

            //获取求职者id
            EmployResume resume = employResumeService.getById(resumeId);
            contract.setPersonId(resume.getUserId());

            //获取实名用户姓名
            EmployAuthenticationPerson person = authenticationPersonService
                    .lambdaQuery()
                    .eq(EmployAuthenticationPerson::getUserId, resume.getUserId())
                    .one();
            contract.setNameB(person.getName());

            //执行合同添加操作
            boolean result = employContractService.save(contract);
            if(result){
                return Result.OK("电子合同添加成功");
            }else {
                return Result.error("电子合同添加失败");
            }

        }catch (Exception e){
            e.printStackTrace();
            return Result.error("电子合同添加失败");
        }
    }

}
