package com.njcn.advance.service.govern.voltage.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.advance.mapper.govern.voltage.SgGovernSchemeHistoryMapper;
import com.njcn.advance.pojo.param.govern.voltage.SgGovernSchemeHistoryParam;
import com.njcn.advance.pojo.po.govern.voltage.SgGovernSchemeHistory;
import com.njcn.advance.pojo.po.govern.voltage.SgProductLine;
import com.njcn.advance.pojo.vo.govern.voltage.SgGovernSchemeHistoryVO;
import com.njcn.advance.service.govern.voltage.ISgGovernSchemeHistoryService;
import com.njcn.advance.service.govern.voltage.ISgProductLineService;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.web.factory.PageFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2024-03-27
 */
@Service
@RequiredArgsConstructor
public class SgGovernSchemeHistoryServiceImpl extends ServiceImpl<SgGovernSchemeHistoryMapper, SgGovernSchemeHistory> implements ISgGovernSchemeHistoryService {


    private final ISgProductLineService productLineService;

    /**
     * 分页查询出治理方案下的历史数据
     *
     * @param sgSchemeHistoryQueryParam 查询条件
     */
    @Override
    public Page<SgGovernSchemeHistoryVO> schemeHistoryList(SgGovernSchemeHistoryParam.SgSchemeHistoryQueryParam sgSchemeHistoryQueryParam) {
        QueryWrapper<SgGovernSchemeHistoryVO> queryWrapper = new QueryWrapper<>();
        List<String> productId = new ArrayList<>();
        if (StrUtil.isNotBlank(sgSchemeHistoryQueryParam.getUserId())) {
            //查询出该用户下所有的生产线
            LambdaQueryWrapper<SgProductLine> sgProductLineLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sgProductLineLambdaQueryWrapper
                    .select(SgProductLine::getId)
                    .eq(SgProductLine::getUserId, sgSchemeHistoryQueryParam.getUserId())
                    .eq(SgProductLine::getState, DataStateEnum.ENABLE.getCode());
            List<SgProductLine> sgProductLineList = productLineService.list(sgProductLineLambdaQueryWrapper);
            if (CollectionUtil.isNotEmpty(sgProductLineList)) {
                productId = sgProductLineList.stream().map(SgProductLine::getId).collect(Collectors.toList());
            }
            if(CollectionUtil.isEmpty(productId)){
                return new Page<>();
            }
        }
        //如果生产线不为空，则加入生产线查询
        if (CollectionUtil.isNotEmpty(productId)) {
            queryWrapper.in("sg_govern_scheme_history.product_line_id", productId);
        }
        queryWrapper.eq("sg_govern_scheme_history.state", DataStateEnum.ENABLE.getCode());
        queryWrapper.orderByDesc("sg_govern_scheme_history.create_time");
        return this.baseMapper.page(new Page<>(PageFactory.getPageNum(sgSchemeHistoryQueryParam), PageFactory.getPageSize(sgSchemeHistoryQueryParam)), queryWrapper);
    }

    @Override
    public boolean deleteScheme(List<String> ids) {
        return this.lambdaUpdate()
                .set(SgGovernSchemeHistory::getState, DataStateEnum.DELETED.getCode())
                .in(SgGovernSchemeHistory::getId, ids)
                .update();
    }
}
