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.ExchangeService;
import org.jeecg.modules.employCert.service.IEmployCertService;
import org.jeecg.modules.employCheckResume.entity.EmployCheckResume;
import org.jeecg.modules.employExchange.entity.EmployExchange;
import org.jeecg.modules.employExchange.service.IEmployExchangeService;
import org.jeecg.modules.employExchangeConfig.entity.EmployExchangeConfig;
import org.jeecg.modules.employExchangeConfig.service.IEmployExchangeConfigService;
import org.jeecg.modules.employExchangeRecord.entity.EmployExchangeRecord;
import org.jeecg.modules.employExchangeRecord.service.IEmployExchangeRecordService;
import org.jeecg.modules.employJob.entity.EmployJob;
import org.jeecg.modules.employScore.entity.EmployScore;
import org.jeecg.modules.employScore.service.IEmployScoreService;
import org.jeecg.modules.employScoreRecord.entity.EmployScoreRecord;
import org.jeecg.modules.employScoreRecord.service.IEmployScoreRecordService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.UUID;

@Service
public class ExchangeServiceImpl implements ExchangeService {

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

    //兑换码信息
    @Resource
    private IHanHaiMemberService hanHaiMemberService;

    //兑换码信息
    @Resource
    private IEmployExchangeService employExchangeService;

    //兑换码使用记录
    @Resource
    private IEmployExchangeRecordService employExchangeRecordService;

    //兑换码问答配置
    @Resource
    private IEmployExchangeConfigService employExchangeConfigService;

    //积分表
    @Resource
    private IEmployScoreService employScoreService;

    //积分记录表
    @Resource
    private IEmployScoreRecordService employScoreRecordService;

    /******************************************************************************************************************/

    //我的服务-查询兑换码信息
    @Override
    public Result<?> queryExchangeList(PageBean pageBean) {
        //返回信息
        String message = "获取兑换码问答配置失败!";

        try{
            //分页
            Page<EmployExchangeConfig> page = new Page<EmployExchangeConfig>(pageBean.getPageNo(), pageBean.getPageSize());
            Page<EmployExchangeConfig> pageList = employExchangeConfigService
                    .lambdaQuery()
                    .page(page);

            //判断执行结果
            if(null != pageList){
                message = "兑换码问答配置信息";
            }else {
                message = "兑换码问答配置为空";
            }

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

        }catch (Exception e){
            //错误信息打印
            e.printStackTrace();
            return Result.error(message);
        }
    }
    //我的服务-使用兑换码
    @Override
    public Result<?> useExchange(String token, String code) {
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            //1、查询兑换码信息
            EmployExchange exchange = employExchangeService
                    .lambdaQuery()
                    .eq(EmployExchange::getCode, code)
                    .eq(EmployExchange::getStatus, "0")
                    .one();
            if(null == exchange){
                return Result.error("该兑换码不存在或已不可再使用");
            }

            //2、修改兑换码状态
            exchange.setStatus("1");//修改兑换码状态为已使用
            boolean exchangeFlag = employExchangeService.updateById(exchange);

            //3、添加兑换码使用记录
            EmployExchangeRecord record = new EmployExchangeRecord();
            record.setCodeId(exchange.getId());
            record.setUserId(hanHaiMember.getId());
            boolean recordFlag = employExchangeRecordService.save(record);

            //4、添加用户积分
            boolean scoreFlag = false;
            BigDecimal score = exchange.getScore();

            BigDecimal newScore = hanHaiMember.getIntegerPrice().add(score);
            hanHaiMember.setIntegerPrice(newScore);

            //修改积分
            scoreFlag = hanHaiMemberService.updateById(hanHaiMember);

            //5、添加用户积分记录
            EmployScoreRecord scoreRecord = new EmployScoreRecord();
            scoreRecord.setScore(score);
            scoreRecord.setUserId(hanHaiMember.getId());
            scoreRecord.setTitle("兑换码获取积分");
            scoreRecord.setType("0");
            boolean scoreRecordFlag = employScoreRecordService.save(scoreRecord);

            boolean result = exchangeFlag && recordFlag && scoreFlag && scoreRecordFlag;

            if(result){
                return Result.OK("兑换码使用成功");
            }else {
                return Result.error("兑换码使用失败");
            }

        }catch (Exception e){
            e.printStackTrace();
            return Result.error("兑换码使用失败");
        }
    }

    //我的服务-生成兑换码
    @Override
    public Result<?> addExchange(EmployExchange employExchange) {

        try{
            UUID code = UUID.randomUUID();//生成兑换码

            EmployExchange exchange = new EmployExchange();
            exchange.setCode(code.toString());//兑换码
            exchange.setStatus("0");//兑换码状态
            exchange.setScore(employExchange.getScore());//兑换码可换取积分数量
            exchange.setOverdueTime(employExchange.getOverdueTime());//兑换码过期时间

            boolean result = employExchangeService.save(exchange);

            if(result){
                return Result.OK("兑换码生成成功");
            }else {
                return Result.error("兑换码使用失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("兑换码使用失败");
        }

    }
}
