package com.zfire.mall.manager.logic.engin.v2.order;

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.PoySaleCountBean;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.engin.v2.order.HomeEnginOrderVO;
import com.zfire.mall.manager.bean.engin.v2.order.TradeEnginOrderVO;
import com.zfire.mall.manager.commonmapper.engin.order.EnginOrderReserveMapper;
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 EnginOrderV2Logic {

    @Resource
    CommonLogic commonLogic;
    @Resource
    EnginOrderReserveMapper enginOrderReserveMapper;

    public IPage<HomeEnginOrderVO> v2HomeListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        this.commonSetParams(zfireParamBean, OrderTypeEnum.HOME.getKey(), adminUser);
        IPage<HomeEnginOrderVO> homeEnginOrderVOIPage = enginOrderReserveMapper.v2HomeListPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
//        if (adminUser.getIsCustomer()) {
//            for (HomeEnginOrderVO record : homeEnginOrderVOIPage.getRecords()) {
//                record.setGeLiNote("");
//                record.setGeLiInerNote("");
//            }
//        }

        return homeEnginOrderVOIPage;
    }



    public IPage<TradeEnginOrderVO> v2TradeListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        this.commonSetParams(zfireParamBean, OrderTypeEnum.TRADE.getKey(), adminUser);
        IPage<TradeEnginOrderVO> tradeEnginOrderVOIPage = enginOrderReserveMapper.v2TradeListPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
//        if (adminUser.getIsCustomer()) {
//            for (TradeEnginOrderVO record : tradeEnginOrderVOIPage.getRecords()) {
//                record.setGeLiNote("");
//                record.setGeLiInerNote("");
//            }
//        }

        return tradeEnginOrderVOIPage;
    }

    private void commonSetParams(ZfireParamBean zfireParamBean, String orderType, AdminUserCom adminUser) {
        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 (!adminUser.getIsCustomer() && CollectionUtils.isNotEmpty(adminUser.getServiceIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("b.service_id")
                    .setCompare("IN")
                    .setValue(adminUser.getServiceIds()));
        }
        if (!adminUser.getIsCustomer() && CollectionUtils.isNotEmpty(adminUser.getK3CategoryIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("b.k3_category_id")
                    .setCompare("IN")
                    .setValue(adminUser.getK3CategoryIds()));
        }
        zfireParamBean.getParams().add(new QueryParamBean().setParam("a.engin_order_type")
                .setCompare("=")
                .setValue(orderType));
        FieldUtils.supplyParam(zfireParamBean);
    }

    public PoySaleCountBean v2HomeCountList(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        this.commonSetParams(zfireParamBean, OrderTypeEnum.HOME.getKey(), adminUser);

        return enginOrderReserveMapper.v2HomeCountList(zfireParamBean);
    }

    public PoySaleCountBean v2TradeCountList(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        this.commonSetParams(zfireParamBean, OrderTypeEnum.TRADE.getKey(), adminUser);

        return enginOrderReserveMapper.v2CountList(zfireParamBean);
    }
}
