package com.muyu.enterpise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.CarFence;
import com.muyu.domain.CarFenceClazz;
import com.muyu.domain.CarFenceType;
import com.muyu.domain.req.CarFenceReq;
import com.muyu.domain.resp.CarFenceResq;
import com.muyu.enterpise.mapper.CarFenceMapper;
import com.muyu.enterpise.service.CarFenceClazzService;
import com.muyu.enterpise.service.CarFenceService;
import com.muyu.enterpise.service.CarFenceTypeService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车辆围栏服务实现类
 *  * @Author：yang
 *  * @Package：com.muyu.server.service.impl
 *  * @Project：cloud-electronic
 *  * @name：CarFenceServiceImpl
 *  * @Date：2024/9/17 16:57
 */
@Service
@Log4j2
public class CarFenceServiceImpl
        extends ServiceImpl<CarFenceMapper, CarFence>
        implements CarFenceService {

    @Autowired
    private CarFenceClazzService carFenceClazzService;
    @Autowired
    private CarFenceTypeService carFenceTypeService;
    /**
     * 查询围栏信息
     */
    @Override
    public Page<CarFenceResq> selectCarFence(CarFenceReq req) {

        LambdaQueryWrapper<CarFence> queryWrapper = new LambdaQueryWrapper<>();
        //数据来源类型
        queryWrapper.eq(
                StringUtils.isNoneEmpty(req.getName()),
                CarFence::getName, req.getName()
        );
        queryWrapper.eq(
                StringUtils.isNoneEmpty(req.getName()),
                CarFence::getName, req.getName()
        );
        queryWrapper.eq(
                StringUtils.isNoneEmpty(req.getName()),
                CarFence::getName, req.getName()
        );

        //使用MyBatis-Plus的分页功能进行查询
        Page<CarFence> carFencePage = this.page(
                //创建分页参数，包括页码和每页显示的记录数
                new Page<>(req.getPageNum(),
                        req.getPageSize()),
                //传递查询条件
                queryWrapper
        );
        List<CarFenceClazz> carFenceClazzList = carFenceClazzService.list()
                .stream()
                .map(CarFenceClazz::carFenceClazzBuild)
                .toList();
        List<CarFenceType> carFenceTypeList = carFenceTypeService.list()
                .stream()
                .map(CarFenceType::carFenceTypeBuild)
                .toList();
        List<CarFence> carFenceList = carFencePage.getRecords();
        this.optimizeEntity(carFenceList,carFenceClazzList,carFenceTypeList);
        //构建一个新的分页响应对象
        return new Page<>() {{
            //从查询结果中获取记录，并使用流式处理将每条记录转换为EtlDataScoreResp对象
            List<CarFenceResq> etlDataScoreRespList = carFencePage.getRecords().stream()
                    //使用map操作将EtlDataScore对象转换为EtlDataScoreResp对象
                    .map(CarFenceResq::carFenceResqListBuilder)
                    //收集流中的元素到列表中
                    .toList();
            //设置分页响应中的记录列表
            setRecords(etlDataScoreRespList);
            //设置分页响应中的总记录数
            setTotal(carFencePage.getTotal());
            //设置分页响应中的当前页码
            setCurrent(carFencePage.getCurrent());
            //设置分页响应中的每页记录数
            setSize(carFencePage.getSize());
        }};
    }


    /**
     * 优化查询返回值
     * @param carFenceList  电子围栏数据
     * @param carFenceClazzList 业务数据
     * @param carFenceTypeList 类型数据
     */
    private void optimizeEntity(
                                List<CarFence> carFenceList,
                                List<CarFenceClazz> carFenceClazzList,
                                List<CarFenceType> carFenceTypeList) {
        carFenceList.forEach(carFence -> {
            carFenceClazzList.forEach(carFenceClazz -> {
                if (carFenceClazz.getClazzId().equals(carFence.getClazzId())) {
                    carFence.setClazzName(carFenceClazz.getClazzName());
                }else {
                    return;
                }
            });
            carFenceTypeList.forEach(carFenceType -> {
                if (carFenceType.getTypeId().equals(carFence.getTypeId())) {
                    carFence.setTypeName(carFenceType.getTypeName());
                }else{
                    return;
                }
            });
        });
    }
}
