package com.muyu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.FaultCode;
import com.muyu.domain.FaultLabel;
import com.muyu.domain.req.FaultCodeAddReq;
import com.muyu.domain.req.FaultCodeListReq;
import com.muyu.domain.req.FaultCodeUpdReq;
import com.muyu.domain.resp.FaultCodeListResp;
import com.muyu.domain.resp.FaultCodeTotalListResp;
import com.muyu.server.mapper.FaultCodeMapper;
import com.muyu.server.service.CarFenceService;
import com.muyu.server.service.CarInformationService;
import com.muyu.server.service.FaultCodeService;
import com.muyu.server.service.FaultLabelService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车辆故障码业务实现层
 * @Author：weiran
 * @Package：com.muyu.cloud.faultmanage.service.impl
 * @Project：cloud-faultmanage
 * @name：FaultCodeServiceImpl
 * @Date：2024/9/17 14:53
 */
@Service
@AllArgsConstructor
public class FaultCodeServiceImpl extends ServiceImpl<FaultCodeMapper, FaultCode> implements FaultCodeService {

    private final FaultCodeMapper faultCodeMapper;
    private final FaultLabelService faultLabelService;
    private final CarInformationService carInformationService;
    private final CarFenceService carFenceService;

    /**
     * 故障码展示(故障码联查)
     * @param faultCodeListReq
     * @return
     */
    @Override
    public FaultCodeTotalListResp selectfaultcodelist(FaultCodeListReq faultCodeListReq) {
        LambdaQueryWrapper<FaultCode> queryWrapper = new LambdaQueryWrapper<>();
        long count = this.count(queryWrapper);

        MPJLambdaWrapper<FaultCode> wrapper = new MPJLambdaWrapper<>();
        MPJLambdaWrapper<FaultCode> eq = wrapper.select(
                        FaultCode::getFaultcodeId,
                        FaultCode::getMessageTypeId,
                        FaultCode::getFaultcodeNumber,
                        FaultCode::getFaultGroup,
                        FaultCode::getFaultValue,
                        FaultCode::getIsWarning)
                .select(
                        FaultLabel::getMessageTypeCode,
                        FaultLabel::getMessageTypeName)
                .leftJoin(FaultLabel.class, FaultLabel::getMessageTypeId, FaultCode::getMessageTypeId)
                .eq(StringUtils.isNotEmpty(faultCodeListReq.getFaultcodeNumber()),
                        FaultCode::getFaultcodeNumber, faultCodeListReq.getFaultcodeNumber())
                .eq(StringUtils.isNotEmpty(faultCodeListReq.getFaultBit()),
                        FaultCode::getFaultBit, faultCodeListReq.getFaultBit());
        wrapper.last("LIMIT "+ ((faultCodeListReq.getPageNum()-1)*faultCodeListReq.getPageSize())+", "+faultCodeListReq.getPageSize());
        List<FaultCodeListResp> faultCodeListResps = faultCodeMapper.selectJoinList(FaultCodeListResp.class, wrapper);
        return FaultCodeTotalListResp.faultCodeTotalListResp(faultCodeListResps,count);
    }


    /**
     * 新增故障码
     * @param faultCodeAddReq
     * @return
     */
    @Override
    public void insert(FaultCodeAddReq faultCodeAddReq) {
        //判断故障名称或故障码是否存在,若都不存在先添加故障名称表，在添加故障码表
        //1.判断故障名称是否存在
        String messageTypeName = faultCodeAddReq.getMessageTypeName();
        FaultLabel faultLabel = faultLabelService.selectfaultName(messageTypeName);
        //2.判断故障码是否存在
        String faultcodeNumber = faultCodeAddReq.getFaultcodeNumber();
        LambdaQueryWrapper<FaultCode> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(faultcodeNumber),
                FaultCode::getFaultcodeNumber, faultcodeNumber);
        List<FaultCode> list = this.list(queryWrapper).stream().map(FaultCode::faultCodeBuilder).toList();
        FaultCode faultCode=null;
        if (!list.isEmpty()){
           faultCode=list.get(0);
        }

        if (faultLabel==null && faultCode==null){

            //添加故障项表
            FaultLabel faultLabel1 = new FaultLabel();
            faultLabel1.setMessageTypeName(faultCodeAddReq.getMessageTypeName());
            faultLabel1.setMessageTypeCode(faultCodeAddReq.getMessageTypeCode());
            faultLabel1.setMessageTypeBelongs(faultCodeAddReq.getMessageTypeBelongs());
            faultLabelService.insertfaultlabel(faultLabel1);
            //添加故障码表
            faultCodeMapper.insert(FaultCode.addfaultcode(faultCodeAddReq));
        }
    }


    /**
     * 修改故障码
     * @param faultCodeUpdReq
     * @return
     */
    @Override
    public void upd(FaultCodeUpdReq faultCodeUpdReq) {
        faultCodeMapper.updateById(FaultCode.updfaultcode(faultCodeUpdReq));
    }


    /**
     * 删除故障码
     * @param messageTypeId
     * @return
     */
    @Override
    public void del(Integer messageTypeId) {
        // 使用LambdaQueryWrapper来构建查询条件
        LambdaQueryWrapper<FaultCode> queryWrapper = new LambdaQueryWrapper<>();
        // 假设FaultCode实体类中有一个getMessageTypeId方法
        queryWrapper.eq(FaultCode::getMessageTypeId, messageTypeId);
        //删除故障码表信息
        faultCodeMapper.delete(queryWrapper);
        //删除对应的故障名称表信息
        faultLabelService.delfaultlabel(messageTypeId);
    }

    /**
     * 故障码展示(故障码联查)
     * @return
     */
    @Override
    public List<FaultCode> faultCodeList(long messageTypeId) {
        return this.list(new LambdaQueryWrapper<FaultCode>()
                .eq(FaultCode::getMessageTypeId,messageTypeId))
                .stream()
                .map(FaultCode::faultCodeBuilder)
                .toList();
    }
}
