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.OrderService;
import org.jeecg.modules.employAuthenticationCompany.entity.EmployAuthenticationCompany;
import org.jeecg.modules.employAuthenticationCompany.service.IEmployAuthenticationCompanyService;
import org.jeecg.modules.employAuthenticationPerson.entity.EmployAuthenticationPerson;
import org.jeecg.modules.employAuthenticationPerson.service.IEmployAuthenticationPersonService;
import org.jeecg.modules.employJob.entity.EmployJob;
import org.jeecg.modules.employJob.service.IEmployJobService;
import org.jeecg.modules.employOrder.entity.EmployOrder;
import org.jeecg.modules.employOrder.service.IEmployOrderService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderServiceImpl implements OrderService {

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

    @Resource
    private IEmployOrderService employOrderService;

    @Resource
    private IEmployJobService employJobService;

    @Resource
    private IEmployAuthenticationCompanyService employAuthenticationCompanyService;

    @Resource
    private IEmployAuthenticationPersonService employAuthenticationPersonService;
    /******************************************************************************************************************/

    //订单信息-查看订单列表
    @Override
    public Result<?> queryOrderList(String token, String status, String role, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //返回信息
        String message = "订单信息列表失败!";
        //订单信息列表
        Page<EmployOrder> pageList = null;
        //分页信息
        Page<EmployOrder> page = null;

        try{
            //分页
            page = new Page<EmployOrder>(pageBean.getPageNo(), pageBean.getPageSize());
            LambdaQueryChainWrapper<EmployOrder> query = employOrderService.lambdaQuery();

            //组装查询信息
            // 用户角色：0-求职方   1-招聘方
            if("0".equals(role)){
                query.eq(EmployOrder::getEmployeeId, hanHaiMember.getId());
            }else if("1".equals(role)){
                query.eq(EmployOrder::getBossId, hanHaiMember.getId());
            }
            //订单状态
            if(null != status){
                query.eq(EmployOrder::getStatus, status);
            }
            //按时间倒序
            query.orderByDesc(EmployOrder::getCreateTime);

            //获取订单信息列表
            pageList = query.page(page);

            for (EmployOrder record : pageList.getRecords()) {
                //招聘工作信息
                EmployJob employJob = employJobService.getById(record.getJobId());
                if(null != employJob){
                    record.setEmployJob(employJob);
                }

                //招聘方企业实名信息
                EmployAuthenticationCompany employAuthenticationCompany = employAuthenticationCompanyService
                        .lambdaQuery()
                        .eq(EmployAuthenticationCompany::getUserId, record.getBossId())
                        .one();
                if(null != employAuthenticationCompany){
                    record.setEmployAuthenticationCompany(employAuthenticationCompany);
                }

                //招聘方个人实名信息
                EmployAuthenticationPerson boss = employAuthenticationPersonService
                        .lambdaQuery()
                        .eq(EmployAuthenticationPerson::getUserId, record.getBossId())
                        .one();
                if(null != boss){
                    record.setEmployAuthenticationPersonBoss(boss);
                }

                //求职方个人实名信息
                EmployAuthenticationPerson employee = employAuthenticationPersonService
                        .lambdaQuery()
                        .eq(EmployAuthenticationPerson::getUserId, record.getEmployeeId())
                        .one();
                if(null != employee){
                    record.setEmployAuthenticationPersonEmployee(employee);
                }
            }

            //判断执行结果
            if(null != pageList){
                message = "订单信息列表";
            }else {
                message = "订单信息列表为空";
            }

            //返回执行结果
            return Result.OK(message, pageList);

        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return Result.error(message, pageList);
        }
    }

    //订单信息-查看订单详情
    @Override
    public Result<?> queryOrderById(String token, String orderId) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //返回信息
        String message = "订单详情查询失败!";

        try{
            EmployOrder employOrder = employOrderService.getById(orderId);

            if(null != employOrder){
                //招聘工作信息
                EmployJob employJob = employJobService.getById(employOrder.getJobId());
                if(null != employJob){
                    employOrder.setEmployJob(employJob);
                }

                //招聘方企业实名信息
                EmployAuthenticationCompany employAuthenticationCompany = employAuthenticationCompanyService
                        .lambdaQuery()
                        .eq(EmployAuthenticationCompany::getUserId, employOrder.getBossId())
                        .one();
                if(null != employAuthenticationCompany){
                    employOrder.setEmployAuthenticationCompany(employAuthenticationCompany);
                }

                //招聘方个人实名信息
                EmployAuthenticationPerson boss = employAuthenticationPersonService
                        .lambdaQuery()
                        .eq(EmployAuthenticationPerson::getUserId, employOrder.getBossId())
                        .one();
                if(null != boss){
                    employOrder.setEmployAuthenticationPersonBoss(boss);
                }

                //求职方个人实名信息
                EmployAuthenticationPerson employee = employAuthenticationPersonService
                        .lambdaQuery()
                        .eq(EmployAuthenticationPerson::getUserId, employOrder.getEmployeeId())
                        .one();
                if(null != employee){
                    employOrder.setEmployAuthenticationPersonEmployee(employee);
                }
            }

            //判断执行结果
            if(null != employOrder){
                message = "订单详情";
            }else {
                message = "该订单不存在";
            }

            //返回执行结果
            return Result.OK(message, employOrder);

        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return Result.error(message);
        }
    }

    //订单信息-添加订单
    @Override
    public Result<?> addOrder(String token, EmployOrder employOrder) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //返回信息
        String message = "订单添加失败";
        //执行结果
        boolean result = false;

        try{
            //3、执行订单信息添加
            result = employOrderService.save(employOrder);

            //判断执行结果
            if(result){
                message = "订单添加成功！";
            }else {
                message = "订单添加失败！";
            }

            //4、返回执行结果
            return Result.OK(message);

        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return Result.error(message);
        }
    }

    //订单信息-取消订单
    @Override
    public Result<?> deleteOrder(String token, String orderId) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //返回信息
        String message = "订单取消失败";
        //执行结果
        boolean result = false;

        try{
            //3、执行订单信息添加
            result = employOrderService.removeById(orderId);

            //判断执行结果
            if(result){
                message = "订单取消成功！";
            }else {
                message = "订单取消失败！";
            }

            //4、返回执行结果
            return Result.OK(message);

        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return Result.error(message);
        }
    }

}
