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.apiBean.ScoreBean;
import org.jeecg.modules.apiService.CommonApiService;
import org.jeecg.modules.employAddress.entity.EmployAddress;
import org.jeecg.modules.employAddress.service.IEmployAddressService;
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.employBillInfo.entity.EmployBillInfo;
import org.jeecg.modules.employBillInfo.service.IEmployBillInfoService;
import org.jeecg.modules.employFeedback.entity.EmployFeedback;
import org.jeecg.modules.employFeedback.service.IEmployFeedbackService;
import org.jeecg.modules.employImage.entity.EmployImage;
import org.jeecg.modules.employImage.service.IEmployImageService;
import org.jeecg.modules.employJobType.entity.EmployJobType;
import org.jeecg.modules.employJobType.service.IEmployJobTypeService;
import org.jeecg.modules.employNature.entity.EmployNature;
import org.jeecg.modules.employNature.service.IEmployNatureService;
import org.jeecg.modules.employNotebook.entity.EmployNotebook;
import org.jeecg.modules.employNotebook.service.IEmployNotebookService;
import org.jeecg.modules.employResume.service.IEmployResumeService;
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.employVipType.entity.EmployVipType;
import org.jeecg.modules.employVipType.service.IEmployVipTypeService;
import org.jeecg.modules.employVipUser.entity.EmployVipUser;
import org.jeecg.modules.employVipUser.service.IEmployVipUserService;
import org.jeecg.modules.employWorkInfo.entity.EmployWorkInfo;
import org.jeecg.modules.employWorkInfo.service.IEmployWorkInfoService;
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.List;

@Service
public class CommonApiServiceImpl implements CommonApiService {

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

    //用户表
    @Resource
    private IHanHaiMemberService hanHaiMemberService;

    //工种类型表
    @Resource
    private IEmployJobTypeService employJobTypeService;

    //工作性质表
    @Resource
    private IEmployNatureService employNatureService;

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

    //图片信息表表
    @Resource
    private IEmployImageService employImageService;

    //账本表
    @Resource
    private IEmployNotebookService employNotebookService;

    //记账信息表
    @Resource
    private IEmployBillInfoService employBillInfoService;

    //记工信息表
    @Resource
    private IEmployWorkInfoService employWorkInfoService;

    //意见反馈表
    @Resource
    private IEmployFeedbackService employFeedbackService;

    //积分表
    @Resource
    private IEmployScoreService employScoreService;

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

    //地址表
    @Resource
    private IEmployAddressService employAddressService;

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

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

    //VIP配置表
    @Resource
    private IEmployVipTypeService employVipTypeService;

    //VIP用户表
    @Resource
    private IEmployVipUserService employVipUserService;


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

    /***************首页***************/
    //获取轮播图列表带分页
    @Override
    public Result<?> queryBannerList(String bannerCategoryId, PageBean pageBean) {

        //商品列表
        Page<EmployImage> pageList = null;
        //分页信息
        Page<EmployImage> page = null;

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

        //组装查询条件
        if(null != bannerCategoryId){
            //车辆类型
            query.eq(EmployImage::getType, bannerCategoryId);
        }

        //获取轮播图列表
        pageList = query.page(page);

        return Result.OK("轮播图列表", pageList);
    }

    //获取工种列表带分页
    @Override
    public Result<?> queryJobTypeList(PageBean pageBean) {

        Page<EmployJobType> page = new Page<EmployJobType>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<EmployJobType> pageList = employJobTypeService
                .lambdaQuery()
                .page(page);

        return Result.OK("工种列表", pageList);
    }

    //获取工作性质带分页
    @Override
    public Result<?> queryJobNatureList(PageBean pageBean) {

        Page<EmployNature> page = new Page<EmployNature>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<EmployNature> pageList = employNatureService
                .lambdaQuery()
                .page(page);

        return Result.OK("工作性质列表", pageList);
    }

    //获取地址列表带分页
    @Override
    public Result<?> queryAddressList(PageBean pageBean) {
        Page<EmployAddress> page = new Page<EmployAddress>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<EmployAddress> pageList = employAddressService
                .lambdaQuery()
                .page(page);

        return Result.OK("地址列表", pageList);
    }

    /***************记工记账***************/
    //记工记账-新建账本
    @Override
    public Result<?> addNoteBook(String token, EmployNotebook employNotebook) {
        String message = "";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        employNotebook.setStatus("0");
        employNotebook.setUserId(hanHaiMember.getId());
        boolean result = employNotebookService.save(employNotebook);

        if(result){
            message = "新建账本成功！";
        }else {
            message = "新建账本失败！";
        }

        return Result.OK(message);
    }

    //记工记账-修改账本
    @Override
    public Result<?> updateNoteBook(String token, EmployNotebook employNotebook) {
        String message = "";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        boolean result = employNotebookService.updateById(employNotebook);

        if(result){
            message = "修改账本成功！";
        }else {
            message = "修改账本失败！";
        }

        return Result.OK(message);
    }

    //记工记账-全年收支
    @Override
    public Result<?> queryBill(String token, String employNoteBookId, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        //获取账本id列表
        Object[] notebooIds = null;
        if(null == employNoteBookId){
            //根据用户id查询用户账本列表
            List<EmployNotebook> notebookList = employNotebookService
                    .lambdaQuery()
                    .eq(EmployNotebook::getUserId, hanHaiMember.getId())
                    .list();

            //获取账本id列表
            int size = (int)notebookList.size();
            notebooIds = new Object[size];
            int index = 0;
            for (EmployNotebook employNotebook : notebookList) {
                notebooIds[index] = employNotebook.getId();
                index++;
            }

            if(size == 0){
                return Result.OK("没有收支记录", notebookList);
            }

        }else {
            notebooIds = new Object[]{employNoteBookId};

            //判断用户输入账本id是否为自己所有
            EmployNotebook employNotebook = employNotebookService
                    .lambdaQuery()
                    .eq(EmployNotebook::getUserId, hanHaiMember.getId())
                    .eq(EmployNotebook::getId, employNoteBookId)
                    .one();
            if(null == employNotebook){
                return Result.OK("该用户没有此账本的收支记录，请检查账本id输入是否错误", employNotebook);
            }
        }

        //根据账本id列表查询收支记录
        Page<EmployBillInfo> page = new Page<EmployBillInfo>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<EmployBillInfo> pageList = employBillInfoService
                .lambdaQuery()
                .in(EmployBillInfo::getNotebookId, notebooIds)
                .page(page);

        return Result.OK("收支记录", pageList);
    }

    //记工记账-技工问题
    @Override
    public Result<?> addQuestion(String token, EmployFeedback employFeedback) {
        String message = "";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        employFeedback.setUserId(hanHaiMember.getId());
        boolean result = employFeedbackService.save(employFeedback);

        if(result){
            message = "技工问题添加成功！";
        }else {
            message = "技工问题添加失败！";
        }

        return Result.OK(message);
    }

    //记工记账-在建结束项目
    @Override
    public Result<?> queryNotebookList(String token, String type, String status, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

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

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

        //获取账本信息
        Page<EmployNotebook> pageList = query.page(page);

        return Result.OK("在建/结束项目", pageList);
    }

    //记工记账-项目记账
    @Override
    public Result<?> addBillInfo(String token, EmployBillInfo employBillInfo) {
        String message = "";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        boolean result = employBillInfoService.save(employBillInfo);

        if(result){
            message = "项目记账添加成功！";
        }else {
            message = "项目记账添加失败！";
        }

        return Result.OK(message);
    }
    //记工记账-项目记工
    @Override
    public Result<?> addWorkInfo(String token, EmployWorkInfo employWorkInfo) {
        String message = "";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        boolean result = employWorkInfoService.save(employWorkInfo);

        if(result){
            message = "项目记工添加成功！";
        }else {
            message = "项目记工添加失败！";
        }

        return Result.OK(message);
    }

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

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

        return Result.OK("积分信息", score);
    }

    //会员中心-积分记录
    @Override
    public Result<?> queryScoreRecord(String token, String type, PageBean pageBean) {
        //权限验证
        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);
    }

    //会员中心-获取VIP配置信息
    @Override
    public Result<?> queryVipType(String type, PageBean pageBean) {
        Page<EmployVipType> page = new Page<EmployVipType>(pageBean.getPageNo(), pageBean.getPageSize());

        //分页
        LambdaQueryChainWrapper<EmployVipType> query = employVipTypeService.lambdaQuery();

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

        //获取VIP配置信息
        Page<EmployVipType> pageList = query
                .page(page);

        return Result.OK("VIP配置信息", pageList);
    }

    //会员中心-开通VIP
    @Override
    public Result<?> addVip(String token, EmployVipUser employVipUser) {
        String message = "";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        employVipUser.setUserId(hanHaiMember.getId());
        //employVipUser.setTypeId(vipTypeId);
        boolean result = employVipUserService.save(employVipUser);

        if(result){
            message = "开通VIP成功！";
        }else {
            message = "开通VIP失败！";
        }

        return Result.OK(message);
    }

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

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

    //我的服务-获取积分-每日签到获取积分
    @Override
    public Result<?> addScoreBySign(String token, Double score) {
        String message = "";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        //查询该用户是否有积分信息
        EmployScore one = employScoreService
                .lambdaQuery()
                .eq(EmployScore::getUserId, hanHaiMember.getId())
                .one();

        //如果有积分信息，就在该基础上增加积分
        EmployScore employScore = new EmployScore();
        employScore.setUserId(hanHaiMember.getId());
        if(null != one){
            double  newScore = one.getScore() + score;

            employScore.setId(one.getId());
            employScore.setScore(newScore);

            //添加/修改积分信息
            boolean result = employScoreService.updateById(employScore);

            if(result){
                message = "积分添加成功！";
            }else {
                message = "积分添加失败！";
            }
        }else {
            //如果没有积分信息，就在新建积分
            employScore.setScore(score);
            boolean result = employScoreService.save(employScore);

            if(result){
                message = "积分添加成功！";
            }else {
                message = "积分添加失败！";
            }
        }

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

        return Result.OK(message);
    }

    //会员中心-个人实名认证
    @Override
    public Result<?> addAuthenticationPerson(String token, EmployAuthenticationPerson employAuthenticationPerson) {
        String message = "";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        employAuthenticationPerson.setUserId(hanHaiMember.getId());
        boolean result = employAuthenticationPersonService.save(employAuthenticationPerson);

        if(result){
            message = "个人实名添加成功！";
        }else {
            message = "个人实名添加失败！";
        }

        return Result.OK(message);
    }

    //会员中心-企业实名认证
    @Override
    public Result<?> addAuthenticationCompany(String token, EmployAhthenticationCompany employAhthenticationCompany) {
        String message = "";
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        employAhthenticationCompany.setUserId(hanHaiMember.getId());
        boolean result = employAhthenticationCompanyService.save(employAhthenticationCompany);

        if(result){
            message = "企业实名添加成功！";
        }else {
            message = "企业实名添加失败！";
        }

        return Result.OK(message);
    }



}
