package cn.com.ty.lift.ud.dataBank.service.impl;

import java.util.List;

import cn.com.ty.lift.ud.dataBank.controller.query.LiftFaultCodeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.ty.lift.ud.dataBank.mapper.LiftBrandMapper;
import cn.com.ty.lift.ud.dataBank.mapper.LiftFaultCodeMapper;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftBrandEntity;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftFaultCodeEntity;
import cn.com.ty.lift.ud.dataBank.service.ILiftFaultCodeService;
import cn.com.xwy.boot.web.dto.RestResponse;

/**
 * 错误代码 -ServiceImpl
 *
 * @author Xson
 * @date 2020-04-23
 */
@Service
@Transactional
public class LiftFaultCodeServiceImpl extends ServiceImpl<LiftFaultCodeMapper, LiftFaultCodeEntity>
    implements ILiftFaultCodeService {

    @Autowired
    private LiftFaultCodeMapper liftFaultCodeMapper;
    @Autowired
    private LiftBrandMapper liftBrandMapper;

    @Override
    public RestResponse selectByExample(LiftFaultCodeQuery query) {
        IPage page = query.page();
        List<LiftFaultCodeEntity> list = liftFaultCodeMapper.selectByQuery(query.condition(), query);
        for (LiftFaultCodeEntity le : list) {
            LiftBrandEntity selectById = liftBrandMapper.selectById(le.getBrandId());
            if (selectById != null) {
                le.setBrandName(selectById.getName());
            }
        }
        int total = liftFaultCodeMapper.countByQuery(query.condition());
        page.setRecords(list);
        page.setTotal(total);
        return RestResponse.success(page, "Queried successfully");
    }

    @Override
    public void updateByIds(List<Long> ids, Integer status) {
        for (Long id : ids) {
            LiftFaultCodeEntity bit = new LiftFaultCodeEntity();
            bit.setId(id);
            bit.setStatuz(status);
            liftFaultCodeMapper.updateById(bit);
        }
    }

    @Override
    public void updateTopFlagByIds(List<Long> ids, Integer status) {
        for (Long id : ids) {
            LiftFaultCodeEntity bit = new LiftFaultCodeEntity();
            bit.setId(id);
            bit.setTopFlag(status);
            liftFaultCodeMapper.updateById(bit);
        }
    }

}
