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


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.admin.AdminUserCom;
import com.gree.mall.manager.bean.websit.SettlementOrderDetail;
import com.gree.mall.manager.bean.websit.SettlementOrderManualDetail;
import com.gree.mall.manager.bean.websit.WorkerSettlementLineVO;
import com.gree.mall.manager.bean.workorder.SettlementOrderManualVO;
import com.gree.mall.manager.bean.workorder.WorkerSettlementManualVO;
import com.gree.mall.manager.commonmapper.workorder.RpMaterialMaper;
import com.gree.mall.manager.enums.SettlementPayTypeEnum;
import com.gree.mall.manager.enums.SettlementStatusEnum;
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.SettlementOrderManualItemService;
import com.gree.mall.manager.plus.service.SettlementOrderManualService;
import com.gree.mall.manager.plus.service.WorkerSettlementManualService;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ManualOrderLogic {


    @Autowired
    RpMaterialMaper rpMaterialMaper;

    @Autowired
    CommonLogic commonLogic;
    
    @Autowired
    SettlementOrderManualService settlementOrderManualService;
    
    @Autowired
    SettlementOrderManualItemService settlementOrderManualItemService;

    @Autowired
    WorkerSettlementManualService workerSettlementManualService;


    public IPage<WorkerSettlementManualVO> list(Page page, ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();

        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean, WorkerSettlementManualVO.class,adminUser);
        IPage<WorkerSettlementManualVO> workerSettlementManualVOIPage = rpMaterialMaper.listManual(new Page(zfireParamBean.getPageNum(),
                        zfireParamBean.getPageSize()),
                zfireParam);

        return workerSettlementManualVOIPage;
    }

    public IPage<SettlementOrderManualVO> listSettlement(Page page, ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();

        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean, SettlementOrderManualVO.class,adminUser);
        IPage<SettlementOrderManualVO> settlementOrderManualVOIPage = rpMaterialMaper.listSettlement(new Page(zfireParamBean.getPageNum(),
                        zfireParamBean.getPageSize()),
                zfireParam);

        return settlementOrderManualVOIPage;
    }

    public SettlementOrderManualDetail detail(String id) {
        SettlementOrderManual settlementOrder = settlementOrderManualService.getById(id);

        SettlementOrderManualDetail settlementOrderDetail = BeanUtil.toBean(settlementOrder, SettlementOrderManualDetail.class);

        settlementOrderDetail.setSettlementOrderManualItems(settlementOrderManualItemService.lambdaQuery()
                .eq(SettlementOrderManualItem::getSettlementOrderId, id).list());
        return settlementOrderDetail;
    }

    @Transactional(rollbackFor = Exception.class)
    public void settlement(List<String> id) {
        if (settlementOrderManualService.lambdaQuery().in(SettlementOrderManual::getSettlementOrderId,id)
                .ne(SettlementOrderManual::getStatus, SettlementStatusEnum.WAIT.getKey()).count() > 0) {
            throw new RemoteServiceException("存在已结算数据");
        }

        List<SettlementOrderManual> settlementOrderManuals = settlementOrderManualService.lambdaQuery()
                .in(SettlementOrderManual::getSettlementOrderId, id)
                .list();

        for (SettlementOrderManual settlementOrder : settlementOrderManuals) {
            WorkerSettlementManual one = workerSettlementManualService.lambdaQuery()
                    .eq(WorkerSettlementManual::getWorkerId, settlementOrder.getWorkerId())
                    .last("limit 1").one();
            one.setOkAmount(one.getOkAmount().add(settlementOrder.getManualAmount()));
            one.setWaitAmount(one.getWaitAmount().subtract(settlementOrder.getManualAmount()));

            one.updateById();
        }

        settlementOrderManualService.lambdaUpdate()
                .in(SettlementOrderManual::getSettlementOrderId, id)
                .set(SettlementOrderManual::getStatus, SettlementStatusEnum.OVER.getKey()).update();

    }
}
