package com.gree.mall.miniapp.logic.apply;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.apply.WorkerApplyCount;
import com.gree.mall.miniapp.bean.apply.WorkerInvolveApplyDetail;
import com.gree.mall.miniapp.bean.apply.WorkerInvolveApplyVO;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.workorder.OrderBaseCMapper;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.PgOrderBase;
import com.gree.mall.miniapp.plus.entity.WorkerInvolveApply;
import com.gree.mall.miniapp.plus.entity.WorkerInvolveApplyItem;
import com.gree.mall.miniapp.plus.service.PgOrderBaseService;
import com.gree.mall.miniapp.plus.service.WorkerInvolveApplyItemService;
import com.gree.mall.miniapp.plus.service.WorkerInvolveApplyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class WorkerApplyLogic {

    private final WorkerInvolveApplyService workerInvolveApplyService;
    private final WorkerInvolveApplyItemService workerInvolveApplyItemService;
    private final PgOrderBaseService pgOrderBaseService;
    private final OrderBaseCMapper orderBaseCMapper;
    private final CommonLogic commonLogic;

    public void add(WorkerInvolveApply workerInvolveApply) {

        workerInvolveApply.insert();

    }

    public void update(WorkerInvolveApply workerInvolveApply) {

        List<WorkerInvolveApplyItem> workerInvolveApplyItems = workerInvolveApplyItemService.lambdaQuery()
                .eq(WorkerInvolveApplyItem::getInvolveApplyId, workerInvolveApply.getId())
                .orderByDesc(WorkerInvolveApplyItem::getCreateTime).list();

        if (CollectionUtil.isEmpty(workerInvolveApplyItems))
            throw new RemoteServiceException("无法重新申请");


        WorkerInvolveApply workerInvolveApplyServiceById = workerInvolveApplyService.getById(workerInvolveApply.getId());

        if (!workerInvolveApplyServiceById.getStatus().equals(2))
            throw new RemoteServiceException("无法重新申请");


        workerInvolveApply.updateById();
    }

    public WorkerInvolveApplyDetail detail(String id) {
        WorkerInvolveApply workerInvolveApply = workerInvolveApplyService.lambdaQuery()
                .eq(WorkerInvolveApply::getOrderBaseId,id)
                .last("limit 1").one();

        if (workerInvolveApply == null)
            return null;

        WorkerInvolveApplyDetail workerInvolveApplyDetail = BeanUtil.toBean(workerInvolveApply, WorkerInvolveApplyDetail.class);

        List<WorkerInvolveApplyItem> workerInvolveApplyItemList = workerInvolveApplyItemService.lambdaQuery()
                .orderByDesc(WorkerInvolveApplyItem::getCreateTime)
                .eq(WorkerInvolveApplyItem::getInvolveApplyId, workerInvolveApply.getId()).list();

        workerInvolveApplyDetail.setWorkerInvolveApplyItems(workerInvolveApplyItemList);


        PgOrderBase pgOrderBase = pgOrderBaseService.getById(workerInvolveApply.getOrderBaseId());

        workerInvolveApplyDetail.setPgOrderBase(pgOrderBase);
        return workerInvolveApplyDetail;
    }

    public IPage<WorkerInvolveApplyVO> list(Integer pageNum, Integer pageSize, List<String> status, String value) {

        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();
        IPage<WorkerInvolveApplyVO> workerInvolveApplyVOIPage = orderBaseCMapper
                .list(new Page(pageNum, pageSize),status,currentCompanyWechat.getUserId(),
                        value);


        return workerInvolveApplyVOIPage;


    }

    public WorkerApplyCount count() {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();
        Integer dsp = workerInvolveApplyService.lambdaQuery()
                .eq(WorkerInvolveApply::getUserId, currentCompanyWechat.getUserId())
                .in(WorkerInvolveApply::getStatus, 1, 3).count();

        Integer ytg = workerInvolveApplyService.lambdaQuery()
                .eq(WorkerInvolveApply::getUserId, currentCompanyWechat.getUserId())
                .in(WorkerInvolveApply::getStatus, 5).count();

        Integer ybh = workerInvolveApplyService.lambdaQuery()
                .eq(WorkerInvolveApply::getUserId, currentCompanyWechat.getUserId())
                .in(WorkerInvolveApply::getStatus,  2, 4).count();

        WorkerApplyCount workerApplyCount = new WorkerApplyCount();
        workerApplyCount.setDsp(dsp);
        workerApplyCount.setYbh(ybh);
        workerApplyCount.setYtg(ytg);
        return workerApplyCount;

    }
}
