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.NotebookService;
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.employNotebook.entity.EmployNotebook;
import org.jeecg.modules.employNotebook.service.IEmployNotebookService;
import org.jeecg.modules.employWorkInfo.entity.EmployWorkInfo;
import org.jeecg.modules.employWorkInfo.service.IEmployWorkInfoService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NotebookServiceImpl implements NotebookService {

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

    //账本表
    @Resource
    private IEmployNotebookService employNotebookService;

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

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

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

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

            //判断是否为修改
            if(null != employNotebook.getId()){
                boolean result = employNotebookService.updateById(employNotebook);
                if(result){
                    message = "修改账本成功！";
                }else {
                    message = "修改账本失败！";
                }
            }

            //新增账本信息
            employNotebook.setUserId(hanHaiMember.getId());
            boolean result = employNotebookService.save(employNotebook);

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

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

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

            boolean result = employNotebookService.updateById(employNotebook);

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

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

    //记工记账-全年收支
    @Override
    public Result<?> queryBill(String token, String employNoteBookId, PageBean pageBean) {

        try{
            //权限验证
            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);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("收支记录获取失败");
        }
    }

    //记工记账-全年收支金额
    public Result<?> queryBillAmount(String token, String employNoteBookId){
        //返回信息
        Map<String, Object> map = new HashMap<>();
        map.put("incomeCount", 0);
        map.put("expenditureCount", 0);

        try{
            //权限验证
            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("没有收支记录", map);
                }

            }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列表查询收支记录
            List<EmployBillInfo> pageList = employBillInfoService
                    .lambdaQuery()
                    .in(EmployBillInfo::getNotebookId, notebooIds)
                    .list();

            BigDecimal incomeCount = BigDecimal.valueOf(0);//总收入
            BigDecimal expenditureCount = BigDecimal.valueOf(0);//总支出
            for (EmployBillInfo employBillInfo : pageList) {
                if(0 == employBillInfo.getType()){
                    incomeCount = incomeCount.add(employBillInfo.getAmount());
                }else if(1 == employBillInfo.getType()){
                    expenditureCount = expenditureCount.add(employBillInfo.getAmount());
                }
            }
            map.put("incomeCount", incomeCount);
            map.put("expenditureCount", expenditureCount);

            return Result.OK("收支/支出金额", map);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("收支记录获取失败");
        }
    }

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

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

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

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

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

        try{
            //分页
            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);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("在建/结束项目列表获取失败");
        }
    }

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

            boolean result = employBillInfoService.save(employBillInfo);

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

            return Result.OK(message);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(message);
        }
    }
    //记工记账-项目记工
    @Override
    public Result<?> addWorkInfo(String token, EmployWorkInfo employWorkInfo) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        String message = "项目记工添加失败！";

        try{
            boolean result = employWorkInfoService.save(employWorkInfo);

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

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

    //记工记账-查询记工信息
    @Override
    public Result<?> queryWork(String token, EmployWorkInfo workInfo, PageBean pageBean){

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

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

            //组装查询条件
            if(null != workInfo.getNotebookId()){
                //关联账本id
                query.eq(EmployWorkInfo::getNotebookId, workInfo.getNotebookId());
            }
            if(null != workInfo.getType()){
                //记账类型
                query.eq(EmployWorkInfo::getType, workInfo.getType());
            }

            //获取记工记录信息
            Page<EmployWorkInfo> pageList = query.page(page);

            return Result.OK("记工记录", pageList);
        }catch (Exception e){
            return Result.error("记工记录查询失败");
        }


    }

}
