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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
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.engin.v2.info.EnginInfoOrderVO;
import com.zfire.mall.manager.bean.engin.v2.info.EnginInfoSaleExecVO;
import com.zfire.mall.manager.commonmapper.engin.info.EnginInfoOrderReserveMapper;
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;
import java.util.List;

@Service
@Slf4j
public class EnginInfoOrderV2Logic {

    @Resource
    CommonLogic commonLogic;
    @Resource
    EnginInfoOrderReserveMapper enginInfoOrderReserveMapper;

    public IPage<EnginInfoOrderVO> v2ListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        List<QueryParamBean> queryParams = zfireParamBean.getParams();
        for (QueryParamBean queryParam : queryParams) {
            if (queryParam.getParam().equals("a.is_close")
                    && queryParam.getValue().equals("false")) {
                queryParams.add(new QueryParamBean().setParam("a.contract_expire_date")
                        .setCompare(">=")
                        .setValue(DateUtil.format(DateUtil.date(), "yyyy-MM-dd") + " 00:00:00"));
                break;
            }
        }
        if (adminUser.getIsCustomer()) {
            queryParams.add(new QueryParamBean().setParam("a.customer_id")
                    .setCompare("=")
                    .setValue(adminUser.getCustomerId()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getAdminCompanyIds())) {
            queryParams.add(new QueryParamBean().setParam("a.admin_company_id")
                    .setCompare("IN")
                    .setValue(adminUser.getAdminCompanyIds()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getServiceIds())) {
            queryParams.add(new QueryParamBean().setParam("a.service_id")
                    .setCompare("IN")
                    .setValue(adminUser.getServiceIds()));
        }
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<EnginInfoOrderVO> page = enginInfoOrderReserveMapper.v2ListPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
//        if (adminUser.getIsCustomer()) {
//            for (EnginInfoOrderVO record : page.getRecords()) {
//                record.setGeLiNote("");
//                record.setGeLiInerNote("");
//            }
//        }
        return page;
    }

    public IPage<EnginInfoSaleExecVO> v2ExecListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (CollectionUtil.isNotEmpty(adminUser.getServiceIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("c.service_id")
                    .setCompare("IN")
                    .setValue(adminUser.getServiceIds()));
        }
        FieldUtils.supplyParam(zfireParamBean);

        Integer total = enginInfoOrderReserveMapper.v2ExecListTotal(zfireParamBean);

        return enginInfoOrderReserveMapper.v2ExecListPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize(),total,false), zfireParamBean);
    }
}
