package org.jeecg.modules.apiService.impl;

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.ExaminationService;
import org.jeecg.modules.employCert.entity.EmployCert;
import org.jeecg.modules.employCert.service.IEmployCertService;
import org.jeecg.modules.employCertMaterial.entity.EmployCertMaterial;
import org.jeecg.modules.employCertMaterial.service.IEmployCertMaterialService;
import org.jeecg.modules.employMaterial.entity.EmployMaterial;
import org.jeecg.modules.employMaterial.service.IEmployMaterialService;
import org.jeecg.modules.employRedister.entity.EmployRedister;
import org.jeecg.modules.employRedister.service.IEmployRedisterService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExaminationServiceImpl implements ExaminationService {

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

    //考证咨询表
    @Resource
    private IEmployCertService employCertService;

    //报名材料表
    @Resource
    private IEmployMaterialService employMaterialService;

    //证书报名材料表
    @Resource
    private IEmployCertMaterialService employCertMaterialService;

    //报名材料提交表
    @Resource
    private IEmployRedisterService employRedisterService;
    /******************************************************************************************************************/

    //考证咨询-证书查询
    @Override
    public Result<?> queryCertList(PageBean pageBean) {
        try{
            Page<EmployCert> page = new Page<EmployCert>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<EmployCert> pageList = employCertService
                    .lambdaQuery()
                    .page(page);

            for (EmployCert record : pageList.getRecords()) {

                //获取证书相关报名材料关联
                List<EmployCertMaterial> employCertMaterialList = employCertMaterialService
                        .lambdaQuery()
                        .eq(EmployCertMaterial::getCertId, record.getId())
                        .list();

                //获取报名材料
                List<EmployMaterial> employMaterialList = new ArrayList<>();
                for (EmployCertMaterial employCertMaterial : employCertMaterialList) {
                    EmployMaterial material = employMaterialService
                            .lambdaQuery()
                            .eq(EmployMaterial::getId, employCertMaterial.getMaterialId())
                            .one();
                    employMaterialList.add(material);
                }
                record.setEmployMaterialList(employMaterialList);

            }
            return Result.OK("证书咨询列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("证书咨询列表查询失败");
        }
    }

    //查询证书详情
    @Override
    public Result<?> queryCertById(String cerId) {

        try{
            //查询证书详情
            EmployCert cert = employCertService.getById(cerId);

            //获取证书相关报名材料关联
            List<EmployCertMaterial> employCertMaterialList = employCertMaterialService
                    .lambdaQuery()
                    .eq(EmployCertMaterial::getCertId, cert.getId())
                    .orderByAsc(EmployCertMaterial::getOrderNo)
                    .list();

            //获取报名材料
            List<EmployMaterial> employMaterialList = new ArrayList<>();
            for (EmployCertMaterial employCertMaterial : employCertMaterialList) {
                EmployMaterial material = employMaterialService
                        .lambdaQuery()
                        .eq(EmployMaterial::getId, employCertMaterial.getMaterialId())
                        .one();
                employMaterialList.add(material);
            }
            cert.setEmployMaterialList(employMaterialList);

            return Result.OK("证书详情", cert);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("证书详情查询失败");
        }
    }

    //考证咨询-提交报名材料
    @Override
    public Result<?> addMaterial(String token, EmployRedister employRedister) {
        //返回信息
        String message = "报名材料提交失败！";

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

            //提交报名材料
            employRedister.setUserId(hanHaiMember.getId());
            boolean result = employRedisterService.save(employRedister);

            //判断招工信息是否添加成功
            if(result){
                message = "报名材料提交成功！";
            }else {
                message = "报名材料提交失败！";
            }
            return Result.OK(message);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(message);
        }
    }

}
