package com.muyu.enterpise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.domain.vo.FaultCodeVo;
import com.muyu.enterpise.mapper.FaultCodeMapper;
import com.muyu.enterpise.service.FaultCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 故障码管理
 *  * @className: FaultCodeServiceImpl  ️✈️
 *  * @author: Yang 鹏  🦅
 *  * @date: 2024/9/23 21:06 ⏰
 *  * @Version: 1.0
 *  * @description: 故障检测
 */
@Service
public class FaultCodeServiceImpl extends ServiceImpl<FaultCodeMapper, FaultCodeVo> implements FaultCodeService {
    @Autowired
    private FaultCodeMapper faultCodeMapper;

    /**
     * 故障码展示
     * @param faultCodeListReq
     * @return
     */
    @Override
    public FaultCodeTotalListResp selectlist(FaultCodeListReq faultCodeListReq) {
        Integer pageNum = faultCodeListReq.getPageNum();
        Integer pageSize = faultCodeListReq.getPageSize();
        LambdaQueryWrapper<FaultCodeVo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(faultCodeListReq.getFaultcodeNumber()),
                FaultCodeVo::getFaultcodeNumber,faultCodeListReq.getFaultcodeNumber());
        queryWrapper.eq(StringUtils.isNotEmpty(faultCodeListReq.getFaultBit()),
                FaultCodeVo::getFaultBit,faultCodeListReq.getFaultBit());
        Long count = this.count(queryWrapper);
        queryWrapper.last("LIMIT "+ ((pageNum-1)*pageSize)+", "+pageSize);
        List<FaultCodeVo> faultCodeVOList = this.list(queryWrapper);
        List<FaultCodeListResp> faultCodeListRespList = faultCodeVOList.stream()
                .map(FaultCodeListResp::faultCodeListResp)
                .toList();
        return FaultCodeTotalListResp.faultCodeTotalListResp(faultCodeListRespList,count);
    }


    /**
     * 故障码展示(故障码联查)
     * @param faultCodeListReq
     * @return
     */
    @Override
    public FaultCodeTotalListResp selectfaultcodelist(FaultCodeListReq faultCodeListReq) {
        LambdaQueryWrapper<FaultCodeVo> queryWrapper = new LambdaQueryWrapper<>();
        Long count = this.count(queryWrapper);
        int offset = (faultCodeListReq.getPageNum() - 1) * faultCodeListReq.getPageSize();
        faultCodeListReq.setPageNum(offset);
        List<FaultCodeVo> selectfaultcodelist = faultCodeMapper.selectfaultcodelist(faultCodeListReq);
        List<FaultCodeListResp> faultCodeListRespList = selectfaultcodelist.stream()
                .map(FaultCodeListResp::faultCodeListResp)
                .toList();
        return FaultCodeTotalListResp.faultCodeTotalListResp(faultCodeListRespList,count);
    }

    /**
     * 新增故障码
     * @param faultCodeAddReq
     * @return
     */
    @Override
    public void insert(FaultCodeAddReq faultCodeAddReq) {
        //判断故障名称或故障码是否存在,若都不存在先添加故障名称表，在添加故障码表
        //1.判断故障名称是否存在
        String messageTypeName = faultCodeAddReq.getMessageTypeName();
        FaultLabel faultLabel = faultCodeMapper.selectfaultName(messageTypeName);
        //2.判断故障码是否存在
        String faultcodeNumber = faultCodeAddReq.getFaultcodeNumber();
        FaultCode faultCode = faultCodeMapper.selectfaultCode(faultcodeNumber);
        if (faultLabel==null && faultCode==null){
            faultCodeMapper.insertfaultlabel(faultCodeAddReq);
            faultCodeMapper.insertfaultcode(faultCodeAddReq);
        }
    }


    /**
     * 修改故障码
     * @param faultCodeUpdReq
     * @return
     */
    @Override
    public void upd(FaultCodeUpdReq faultCodeUpdReq) {
        //修改故障名称表信息和故障码表信息
//        faultCodeMapper.updfaultlabel(faultCodeUpdReq);
        faultCodeMapper.updfaultcode(faultCodeUpdReq);
    }


    /**
     * 删除故障码
     * @param messageTypeId
     * @return
     */
    @Override
    public void del(Integer messageTypeId) {

        //删除故障码表信息
        faultCodeMapper.delfaultcode(Long.valueOf(messageTypeId));
        //删除对应的故障名称表信息
        faultCodeMapper.delfaultlabel(Long.valueOf(messageTypeId));
    }
}
