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.ScoreService;
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.time.LocalDate;

@Service
public class ScoreServiceImpl implements ScoreService {
    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;

    //积分记录表
    @Resource
    private IHanHaiMemberService hanHaiMemberService;

    //积分表
    @Resource
    private IEmployScoreService employScoreService;

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

    //会员中心-积分
    @Override
    public Result<?> queryScore(String token) {
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            //获取积分
            EmployScore score = employScoreService
                    .lambdaQuery()
                    .eq(EmployScore::getUserId, hanHaiMember.getId())
                    .one();

            return Result.OK("积分信息", score);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("积分信息获取失败");
        }
    }

    //会员中心-积分记录
    @Override
    public Result<?> queryScoreRecord(String token, String type, PageBean pageBean) {
        try{
            //权限验证
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

            //分页
            Page<EmployScoreRecord> page = new Page<EmployScoreRecord>(pageBean.getPageNo(), pageBean.getPageSize());
            LambdaQueryChainWrapper<EmployScoreRecord> query = employScoreRecordService.lambdaQuery();

            //组装查询条件
            query.eq(EmployScoreRecord::getUserId, hanHaiMember.getId());
            if(null != type){
                query.eq(EmployScoreRecord::getType, type);
            }

            //获取积分记录
            Page<EmployScoreRecord> pageList = query.page(page);

            return Result.OK("积分记录", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("积分记录获取失败");
        }
    }

    //我的服务-获取积分-邀请工友获取积分
    @Override
    public Result<?> addScoreByShare(String token, String score) {
        //积分表
        //根据userId查询出用户正式积分
        //修改正式积分

        //积分记录表
        return Result.OK("该接口暂未完成");
    }

    //我的服务-获取积分-每日签到获取积分
    @Override
    public Result<?> addScoreBySign(String token) {
        String message = "积分添加失败！";

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

            //判断今日是否已签到
            boolean flag = judgeSign(token);
            if(flag){
                return Result.error("已签到，不可重复签到");
            }

            BigDecimal score = BigDecimal.valueOf(2);//签到积分

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

            //修改积分
            boolean result = hanHaiMemberService.updateById(hanHaiMember);
            if(result){
                message = "积分添加成功！";
            }else {
                message = "积分添加失败！";
            }

            //积分记录添加
            EmployScoreRecord record = new EmployScoreRecord();
            record.setScore(score);
            record.setUserId(hanHaiMember.getId());
            record.setTitle("每日签到获取积分");
            record.setType("0");
            employScoreRecordService.save(record);

            return Result.OK(message);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(message);
        }
    }

    //判断当日是否已签到
    @Override
    public Boolean judgeSign(String token) {
        Boolean result = true;

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

            //获取当天日期
            LocalDate date = LocalDate.now();

            //查询当日签到记录
            EmployScoreRecord record = employScoreRecordService
                    .lambdaQuery()
                    .eq(EmployScoreRecord::getUserId, hanHaiMember.getId())
                    .like(EmployScoreRecord::getCreateTime, date)
                    .eq(EmployScoreRecord::getTitle, "每日签到获取积分")
                    .one();

            if(null != record){
                result = true;
            } else {
                result = false;
            }

            return result;
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }
    }


}
