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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.activity.PromotionActivityBatchBean;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.apply.WorkerInvolveApplyDetail;
import com.gree.mall.manager.bean.apply.WorkerInvolveApplyVO;
import com.gree.mall.manager.bean.apply.WorkerInvolvePrincipalDetail;
import com.gree.mall.manager.bean.apply.WorkerInvolvePrincipalVO;
import com.gree.mall.manager.bean.es.ESGoodsVO;
import com.gree.mall.manager.commonmapper.CommonMapper;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.*;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class WorkerInvolveLogic {

    private final CommonLogic commonLogic;
    private final CommonMapper commonMapper;
    private final WorkerInvolvePrincipalService workerInvolvePrincipalService;
    private final WorkerInvolvePrincipalItemService workerInvolvePrincipalItemService;
    private final WorkerInvolveApplyService workerInvolveApplyService;
    private final WorkerInvolveApplyItemService workerInvolveApplyItemService;
    private final PgOrderBaseService pgOrderBaseService;

    public IPage<WorkerInvolvePrincipalVO> list(ZfireParamBean zfireParamBean) {

        AdminUserCom adminUser = commonLogic.getAdminUser();
        FieldUtils.supplyParam(zfireParamBean, WorkerInvolvePrincipalVO.class, adminUser);
        IPage<WorkerInvolvePrincipalVO> page = commonMapper.principalPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;
    }

    public WorkerInvolvePrincipalDetail detail(String id) {
        WorkerInvolvePrincipal workerInvolvePrincipal = workerInvolvePrincipalService.getById(id);

        WorkerInvolvePrincipalDetail workerInvolvePrincipalDetail = BeanUtil.toBean(workerInvolvePrincipal, WorkerInvolvePrincipalDetail.class);

        List<WorkerInvolvePrincipalItem> workerInvolve = workerInvolvePrincipalItemService.lambdaQuery().eq(WorkerInvolvePrincipalItem::getPrincipalId, id).list();

        workerInvolvePrincipalDetail.setWorkerInvolvePrincipalItems(workerInvolve);

        return workerInvolvePrincipalDetail;
    }

    public void add(WorkerInvolvePrincipalDetail bean) {

        if (workerInvolvePrincipalService.lambdaQuery().eq(WorkerInvolvePrincipal::getPrincipal,bean.getId())
        .count() > 0)
            throw new RemoteServiceException("存在相同审批人名称");

        bean.insert();

        for (WorkerInvolvePrincipalItem workerInvolvePrincipalItem : bean.getWorkerInvolvePrincipalItems()) {
            workerInvolvePrincipalItem.setPrincipalId(bean.getId());
        }

        workerInvolvePrincipalItemService.saveBatch(bean.getWorkerInvolvePrincipalItems());
    }

    public void update(WorkerInvolvePrincipalDetail bean) {


        if (workerInvolvePrincipalService.lambdaQuery().eq(WorkerInvolvePrincipal::getPrincipal,bean.getId())
                .ne(WorkerInvolvePrincipal::getId,bean.getId())
                .count() > 0)
            throw new RemoteServiceException("存在相同审批人名称");

        bean.updateById();

        workerInvolvePrincipalItemService.lambdaUpdate()
                .eq(WorkerInvolvePrincipalItem::getPrincipalId,bean.getId())
                .remove();

        for (WorkerInvolvePrincipalItem workerInvolvePrincipalItem : bean.getWorkerInvolvePrincipalItems()) {
            workerInvolvePrincipalItem.setPrincipalId(bean.getId());
        }

        workerInvolvePrincipalItemService.saveBatch(bean.getWorkerInvolvePrincipalItems());
    }

    public IPage<WorkerInvolveApplyVO> listApply(ZfireParamBean zfireParamBean) {

        AdminUserCom adminUser = commonLogic.getAdminUser();
        FieldUtils.supplyParam(zfireParamBean, WorkerInvolveApplyVO.class, adminUser);
        IPage<WorkerInvolveApplyVO> page = commonMapper.listApply(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;

    }

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

        if (workerInvolveApply == null)
            return null;

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

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

        workerInvolveApplyDetail.setWorkerInvolveApplyItems(workerInvolveApplyItemList);


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

        workerInvolveApplyDetail.setPgOrderBase(pgOrderBase);
        return workerInvolveApplyDetail;
    }

    public void websitAppeal(WorkerInvolveApplyItem workerInvolveApplyItem) {

        WorkerInvolveApply workerInvolveApply = workerInvolveApplyService.getById(workerInvolveApplyItem.getInvolveApplyId());

        workerInvolveApply.setStatus(workerInvolveApplyItem.getWebsitStatus());

        workerInvolveApply.updateById();

        workerInvolveApplyItem.insert();
    }

    public void appeal(WorkerInvolveApplyItem workerInvolveApplyItem) {

        if (!workerInvolveApplyItem.getWebsitStatus().equals(5))
            throw new RemoteServiceException("网点未审批通过");


        WorkerInvolveApply workerInvolveApply = workerInvolveApplyService.getById(workerInvolveApplyItem.getInvolveApplyId());

        workerInvolveApply.setStatus(workerInvolveApplyItem.getStatus());

        workerInvolveApply.updateById();

        workerInvolveApplyItem.updateById();
    }
}
