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

import cn.com.ty.lift.ud.maintenanceCompany.controller.query.MaintenanceCompanyQuery;
import cn.com.ty.lift.ud.maintenanceCompany.mapper.MaintenanceCompanyMapper;
import cn.com.ty.lift.ud.maintenanceCompany.mapper.entity.MaintenanceCompanyEntity;
import cn.com.ty.lift.ud.maintenanceCompany.service.IMaintenanceCompanyService;
import cn.com.ty.lift.ud.maintenanceCompany.mapper.entity.MaintenanceCompanyVo;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.com.xwy.util.collection.ListUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 维保公司-ServiceImpl
 *
 * @author Xson
 * @date 2020-05-19
 */
@Service
@Transactional
public class MaintenanceCompanyServiceImpl extends ServiceImpl<MaintenanceCompanyMapper, MaintenanceCompanyEntity>
    implements IMaintenanceCompanyService {

    @Autowired
    private MaintenanceCompanyMapper maintenanceCompanyMapper;

    /**
     * 查询所有公司名称和ID
     * @param query
     * @return
     */
    @Override
    public RestResponse getAllCompany(MaintenanceCompanyQuery query) {
        List<MaintenanceCompanyVo> companyList = new ArrayList<>();
        IPage page = query.page();
        List<MaintenanceCompanyEntity> list = maintenanceCompanyMapper.selectByQuery(query.condition(), query);
        if(ListUtil.isNotEmpty(list)){
            //抽取数据
            for(MaintenanceCompanyEntity mcEntity:list){
                MaintenanceCompanyVo mcVo  =new MaintenanceCompanyVo();
                mcVo.setId(mcEntity.getId());
                mcVo.setName(mcEntity.getName());
                companyList.add(mcVo);
            }
        }
        page.setRecords(companyList);
        int total = maintenanceCompanyMapper.countByQuery(query.condition());
        page.setTotal(total);
        return RestResponse.success(page, "Select successfully");
    }
}
