package com.gree.mall.miniapp.logic.engin;

import cn.hutool.core.bean.BeanUtil;
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.gree.mall.miniapp.bean.engin.RpProjectRepairBean;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.enums.UserTypeEnum;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.*;
import com.gree.mall.miniapp.plus.service.*;
import com.gree.mall.miniapp.utils.StringUtil;
import com.gree.mall.miniapp.plus.entity.RpProjectRepair;
import com.gree.mall.miniapp.plus.entity.RpProjectRepairBy;
import com.gree.mall.miniapp.plus.entity.RpProjectRepairProduct;
import com.gree.mall.miniapp.plus.entity.RpProjectRepairWebsit;
import com.gree.mall.miniapp.plus.service.RpProjectRepairByService;
import com.gree.mall.miniapp.plus.service.RpProjectRepairProductService;
import com.gree.mall.miniapp.plus.service.RpProjectRepairService;
import com.gree.mall.miniapp.plus.service.RpProjectRepairWebsitService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：lijh
 * @description：工程维保
 * @date ：2024/1/15 15:51
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ProjectRepairLogic {

    private final CommonLogic commonLogic;
    private final RpProjectRepairService rpProjectRepairService;
    private final RpProjectRepairProductService rpProjectRepairProductService;
    private final RpProjectRepairWebsitService rpProjectRepairWebsitService;
    private final RpProjectRepairByService rpProjectRepairByService;
    private final WebsitUserService websitUserService;

    /**
     * 工程维保-列表
     * @return
     */
    public IPage<RpProjectRepair> list(String id, Integer pageNum, Integer pageSize){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();
        if (StringUtil.isEmpty(currentCompanyWechat.getUser().getMobile()))
            return new Page<>();

        List<String> websitS = websitUserService.lambdaQuery().eq(WebsitUser::getUserId, currentCompanyWechat.getUserId())
                .list().stream().map(WebsitUser::getWebsitId).collect(Collectors.toList());

        //非普通用户筛选网点
        List<String> rpId =new ArrayList<>();
        if (!CollectionUtils.isEmpty(websitS)){

            rpId = rpProjectRepairWebsitService.lambdaQuery().in(RpProjectRepairWebsit::getWebsitId, websitS)
                    .list().stream().map(RpProjectRepairWebsit::getProjectRepairId).collect(Collectors.toList());
        }


        //用户项目人员可以创建项目
        List<String> collect = rpProjectRepairByService.lambdaQuery().eq(RpProjectRepairBy::getMobile, currentCompanyWechat.getUser().getMobile())
                .select(RpProjectRepairBy::getProjectRepairId).list().stream().map(RpProjectRepairBy::getProjectRepairId).collect(Collectors.toList());

        //普通用户，项目为空
        if (CollectionUtils.isEmpty(collect) && currentCompanyWechat.getUser().getType().equals(UserTypeEnum.GENERAL.getKey()))
            return new Page<>();

        //非普通用户，网点筛选为空，项目为空
        if (CollectionUtils.isEmpty(rpId) && !currentCompanyWechat.getUser().getType().equals(UserTypeEnum.GENERAL.getKey()))
            return new Page<>();

        Page<RpProjectRepair> page = rpProjectRepairService.lambdaQuery()
                .eq(RpProjectRepair::getCompanyWechatId, currentCompanyWechat.getCompanyWechatId())
                .eq(StringUtils.isNotBlank(id), RpProjectRepair::getId, id)

                .in(currentCompanyWechat.getUser().getType().equals(UserTypeEnum.GENERAL.getKey()),RpProjectRepair::getId, collect)
                //非用户，筛选网点
                .in(!currentCompanyWechat.getUser().getType().equals(UserTypeEnum.GENERAL.getKey()),RpProjectRepair::getId, rpId)

                .le(RpProjectRepair::getStartTime, DateUtil.formatDate(DateUtil.date()) + " 00:00:00")
                .ge(RpProjectRepair::getEndTime, DateUtil.formatDate(DateUtil.date()) + " 00:00:00")
//                .apply("'" + DateUtil.formatDate(DateUtil.date()) + " 00:00:00' BETWEEN start_time AND end_time" )
                .orderByDesc(RpProjectRepair::getId)
                .page(new Page<>(pageNum, pageSize));
        return page;
    }


    /**
     * 工程维保-详情
     * @param id
     * @return
     */
    public RpProjectRepairBean detail(String id){
        RpProjectRepair rpProjectRepair = rpProjectRepairService.getById(id);
        RpProjectRepairBean rpProjectRepairBean = BeanUtil.copyProperties(rpProjectRepair, RpProjectRepairBean.class);
        //服务网点
        List<RpProjectRepairWebsit> list = rpProjectRepairWebsitService.lambdaQuery().eq(RpProjectRepairWebsit::getProjectRepairId, id).list();
        rpProjectRepairBean.setProjectRepairWebsitList(list);
        return rpProjectRepairBean;
    }


    /**
     * 工程维保-去重后的品牌
     * @param projectRepairId
     * @return
     */
    public List<RpProjectRepairProduct> brand(String projectRepairId){
        List<RpProjectRepairProduct> list = rpProjectRepairProductService.lambdaQuery()
                .select(RpProjectRepairProduct::getBrandId,RpProjectRepairProduct::getBrandName)
                .eq(RpProjectRepairProduct::getProjectRepairId, projectRepairId)
                .list().stream().distinct().collect(Collectors.toList());
        return list;
    }

    /**
     * 工程维保-去重后的产品大类
     * @param projectRepairId
     * @return
     */
    public List<RpProjectRepairProduct> main(String projectRepairId,String brandId){
        List<RpProjectRepairProduct> list = rpProjectRepairProductService.lambdaQuery()
                .select(RpProjectRepairProduct::getMainId, RpProjectRepairProduct::getMainName)
                .eq(RpProjectRepairProduct::getProjectRepairId, projectRepairId)
                .eq(StringUtils.isNotBlank(brandId),RpProjectRepairProduct::getBrandId, brandId)
                .list().stream().distinct().collect(Collectors.toList());
        return list;
    }

    /**
     * 工程维保-去重后的产品小类
     * @param projectRepairId
     * @return
     */
    public List<RpProjectRepairProduct> small(String projectRepairId,String brandId,String mainId){
        List<RpProjectRepairProduct> list = rpProjectRepairProductService.lambdaQuery()
                .select(RpProjectRepairProduct::getSmallId, RpProjectRepairProduct::getSmallName,
                        RpProjectRepairProduct::getMainName,RpProjectRepairProduct::getMainId,RpProjectRepairProduct::getSmallImg)
                .eq(RpProjectRepairProduct::getProjectRepairId, projectRepairId)
                .eq(StringUtils.isNotBlank(brandId),RpProjectRepairProduct::getBrandId, brandId)
                .eq(StringUtils.isNotBlank(mainId),RpProjectRepairProduct::getMainId,mainId)
                .list().stream().distinct().collect(Collectors.toList());
        return list;
    }

}
