package com.zfire.mall.manager.logic.front;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.SqlParser;
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.front.CustomerCodeInstallVO;
import com.zfire.mall.manager.bean.front.CustomerOrderBean;
import com.zfire.mall.manager.bean.front.CustomerOrderInstallDetail;
import com.zfire.mall.manager.bean.front.CustomerStockBean;
import com.zfire.mall.manager.commonmapper.front.CustomerFrontMapper;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.CustomerOrderInstall;
import com.zfire.mall.manager.plus.entity.CustomerOrderInstallLog;
import com.zfire.mall.manager.plus.entity.KingDeeCustomer;
import com.zfire.mall.manager.plus.service.CustomerOrderInstallLogService;
import com.zfire.mall.manager.plus.service.CustomerOrderInstallService;
import com.zfire.mall.manager.plus.service.KingDeeCustomerService;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author changelin
 */
@Service
@Slf4j
public class CustomerOrderInstallLogic {

    @Autowired
    CustomerOrderInstallService customerOrderInstallService;

    @Autowired
    CustomerOrderInstallLogService customerOrderInstallLogService;

    @Autowired
    CustomerFrontMapper customerFrontMapper;

    @Autowired
    KingDeeCustomerService kingDeeCustomerService;

    @Autowired
    CommonLogic commonLogic;




    public CustomerOrderInstallDetail detail(String gongDanId) {
        CustomerOrderInstall customerOrderInstall = customerOrderInstallService.getById(gongDanId);
        if (customerOrderInstall == null)
            return null;
        List<CustomerOrderInstallLog> customerOrderInstallLogList = customerOrderInstallLogService.lambdaQuery().eq(CustomerOrderInstallLog::getWorkerOrderNo, gongDanId).list();
        CustomerOrderInstallDetail customerOrderInstallDetail = BeanUtil.toBean(customerOrderInstall, CustomerOrderInstallDetail.class);
        customerOrderInstallDetail.setLogs(customerOrderInstallLogList);
        return customerOrderInstallDetail;

    }

    public IPage<CustomerCodeInstallVO> list(Page page, ZfireParamBean zfireParam) {

        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> customers=new ArrayList<>();
        if (adminUser.getIsCustomer()) {
            KingDeeCustomer kingDeeCustomer = kingDeeCustomerService.getById(adminUser.getCustomerId());
            customers.add(kingDeeCustomer.getName());
            if (kingDeeCustomer.getJiaxianType().equals("KING")){
                List<KingDeeCustomer> list = kingDeeCustomerService.lambdaQuery().eq(KingDeeCustomer::getHigherPrantNumber, adminUser.getCustomerNumber()).list();
                if (CollectionUtils.isNotEmpty(list)){
                    list.stream().forEach(item -> customers.add(item.getName()));
                }
            }
        }
        IPage<CustomerCodeInstallVO> customerStockBeanIPage = customerFrontMapper.listCode(page, zfireParam, customers);
        return customerStockBeanIPage;

    }
}
