package com.zfire.mall.manager.logic.record.order.exec.v2;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.record.order.exec.v2.OrderExecRecordVO;
import com.zfire.mall.manager.bean.record.order.exec.v2.RetailOrderExecRecordVO;
import com.zfire.mall.manager.bean.record.order.exec.v2.TradeEOrderExecRecordVO;
import com.zfire.mall.manager.commonmapper.record.order.exec.OrderExecRecordReserveMapper;
import com.zfire.mall.manager.enums.OrderTypeEnum;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.zfire.bean.QueryParamBean;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderExecRecordV2Logic {

    @Resource
    OrderExecRecordReserveMapper orderExecRecordReserveMapper;
    @Resource
    CommonLogic commonLogic;

    public IPage<RetailOrderExecRecordVO> v2RetailExecListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        if (adminUser.getIsCustomer()) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.customer_id")
                    .setCompare("=")
                    .setValue(adminUser.getCustomerId()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getAdminCompanyIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.admin_company_id")
                    .setCompare("IN")
                    .setValue(adminUser.getAdminCompanyIds()));
        }
        zfireParamBean.getParams().add(new QueryParamBean().setParam("a.type")
                .setCompare("=")
                .setValue("2"));
        FieldUtils.supplyParam(zfireParamBean);
        IPage<RetailOrderExecRecordVO> page = orderExecRecordReserveMapper.v2RetailListPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;
    }

    public IPage<RetailOrderExecRecordVO> v2RetailOtherExecListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        if (adminUser.getIsCustomer()) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.customer_id")
                    .setCompare("=")
                    .setValue(adminUser.getCustomerId()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getAdminCompanyIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.admin_company_id")
                    .setCompare("IN")
                    .setValue(adminUser.getAdminCompanyIds()));
        }
        zfireParamBean.getParams().add(new QueryParamBean().setParam("a.type")
                .setCompare("=")
                .setValue("1"));
        FieldUtils.supplyParam(zfireParamBean);
        IPage<RetailOrderExecRecordVO> page = orderExecRecordReserveMapper.v2RetailListPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;
    }

    public IPage<TradeEOrderExecRecordVO> v2TradeEExecListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        if (adminUser.getIsCustomer()) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.customer_id")
                    .setCompare("=")
                    .setValue(adminUser.getCustomerId()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getAdminCompanyIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.admin_company_id")
                    .setCompare("IN")
                    .setValue(adminUser.getAdminCompanyIds()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getServiceIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.service_id")
                    .setCompare("IN")
                    .setValue(adminUser.getServiceIds()));
        }
        zfireParamBean.getParams().add(new QueryParamBean().setParam("a.engin_order_type")
                .setCompare("=")
                .setValue(OrderTypeEnum.TRADE.getKey()));
        FieldUtils.supplyParam(zfireParamBean);
        IPage<TradeEOrderExecRecordVO> page = orderExecRecordReserveMapper.v2TradeEExecListPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;
    }

    public IPage<OrderExecRecordVO> v2ExecListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        if (adminUser.getIsCustomer()) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.customer_id")
                    .setCompare("=")
                    .setValue(adminUser.getCustomerId()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getAdminCompanyIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.admin_company_id")
                    .setCompare("IN")
                    .setValue(adminUser.getAdminCompanyIds()));
        }
        FieldUtils.supplyParam(zfireParamBean);
        IPage<OrderExecRecordVO> page = orderExecRecordReserveMapper.v2ExecListPage(new Page(zfireParamBean.getPageNum(),
                zfireParamBean.getPageSize()), zfireParamBean, adminUser.getServiceIds());
        return page;
    }
}
