package com.unis.base.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.*;
import com.unis.base.server.domain.FacilityDomain;
import com.unis.base.server.service.FacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author ChenKai
 * @Date 2019/9/19 18:39
 * @Version 1.0
 */

@Service
public class FacilityServiceImpl implements FacilityService {

    @Autowired
    private FacilityDomain facilityDomain;

    //设备报废记录
    @Override
    public List<DsbbfxxVO> findFacility(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize, Integer lx) {
        PageHelper.startPage(pageNum,pageSize);
        return facilityDomain.findFacility(vo,lx);
    }

    //设备报废记录详情
    @Override
    public MachineVO findFacilityById(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<DsbbfxxVO> dsbbfxxVOList = facilityDomain.findFacilityById(vo);
        PageInfo<DsbbfxxVO> pageInfos = new PageInfo<DsbbfxxVO>(dsbbfxxVOList);
        MachineVO machineVO = new MachineVO();
        if (dsbbfxxVOList!=null){
            DsbbfxxVO dsbbfxxVO = dsbbfxxVOList.get(0);
            machineVO.setQymc(dsbbfxxVO.getQymc());
            machineVO.setFddbr(dsbbfxxVO.getFddbr());
            machineVO.setBgdh(dsbbfxxVO.getBgdh());
            machineVO.setKdmc(dsbbfxxVO.getKdmc());
            machineVO.setKddz(dsbbfxxVO.getKddz());
        }else {
            return null;
        }
        machineVO.setDsbbfxxVOPageInfo(pageInfos);
        //machineVO.setDsbbfxxVOList(dsbbfxxVOList);
        return machineVO;
    }

    //设备维护分页
    @Override
    public List<DsbwhxxVO> findVindicate(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize, Integer lx) {
        PageHelper.startPage(pageNum,pageSize);
        return facilityDomain.findVindicate(vo,lx);
    }

    //设备维护详情
    @Override
    public MachineVO findVindicateById(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<DsbwhxxVO> vindicateById = facilityDomain.findVindicateById(vo);
        PageInfo<DsbwhxxVO> pageInfos = new PageInfo<DsbwhxxVO>(vindicateById);
        MachineVO machineVO = new MachineVO();
        if (vindicateById!=null){
            DsbwhxxVO dsbwhxxVO = vindicateById.get(0);
            machineVO.setQymc(dsbwhxxVO.getQymc());
            machineVO.setFddbr(dsbwhxxVO.getFddbr());
            machineVO.setBgdh(dsbwhxxVO.getBgdh());
            machineVO.setKdmc(dsbwhxxVO.getKdmc());
            machineVO.setKddz(dsbwhxxVO.getKddz());
        }else {
            return null;
        }
        machineVO.setDsbwhxxVOPageInfo(pageInfos);
        //machineVO.setDsbwhxxVOList(vindicateById);
        return machineVO;
    }

    //设备维修分页
    @Override
    public List<DSbwxxxVO> findMaintain(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize, Integer lx) {
        PageHelper.startPage(pageNum,pageSize);
        return facilityDomain.findMaintain(vo,lx);
    }

    //设备维修详情
    @Override
    public MachineVO findMaintainById(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<DSbwxxxVO> dSbwxxxVOList = facilityDomain.findMaintainById(vo);
        PageInfo<DSbwxxxVO> pageInfos = new PageInfo<DSbwxxxVO>(dSbwxxxVOList);
        MachineVO machineVO = new MachineVO();
        if (dSbwxxxVOList!=null){
            DSbwxxxVO dSbwxxxVO = dSbwxxxVOList.get(0);
            machineVO.setQymc(dSbwxxxVO.getQymc());
            machineVO.setFddbr(dSbwxxxVO.getFddbr());
            machineVO.setBgdh(dSbwxxxVO.getBgdh());
            machineVO.setKdmc(dSbwxxxVO.getKdmc());
            machineVO.setKddz(dSbwxxxVO.getKddz());
        }else {
            return null;
        }
        machineVO.setDSbwxxxVOPageInfo(pageInfos);
        //machineVO.setDSbwxxxVOList(dSbwxxxVOList);
        return machineVO;
    }

    //设备领用
    @Override
    public List<DsblyxxVO> findReceive(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize, Integer lx) {
        PageHelper.startPage(pageNum,pageSize);
        return facilityDomain.findReceive(vo,lx);
    }

    //设备领用详情
    @Override
    public MachineVO findReceiveById(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<DsblyxxVO> dsblyxxVOList = facilityDomain.findReceiveById(vo);
        PageInfo<DsblyxxVO> pageInfos = new PageInfo<DsblyxxVO>(dsblyxxVOList);
        List<DsblyxxVO> guiHuanList = facilityDomain.findGuiHuan(vo);
        PageInfo<DsblyxxVO> voPageInfo = new PageInfo<>(guiHuanList);
        MachineVO machineVO = new MachineVO();
        if (dsblyxxVOList!=null){
            DsblyxxVO dsblyxxVO = dsblyxxVOList.get(0);
            machineVO.setQymc(dsblyxxVO.getQymc());
            machineVO.setFddbr(dsblyxxVO.getFddbr());
            machineVO.setBgdh(dsblyxxVO.getBgdh());
            machineVO.setKdmc(dsblyxxVO.getKdmc());
            machineVO.setKddz(dsblyxxVO.getKddz());
        }else {
            return null;
        }
        //machineVO.setDsblyxxVOList(dsblyxxVOList);
        machineVO.setGuiHuanPageInfo(voPageInfo);
        machineVO.setDsblyxxVOPageInfo(pageInfos);
        return machineVO;
    }

    //设备入库
    @Override
    public List<DSbxxsjyVO> findStorage(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize, Integer lx) {
        PageHelper.startPage(pageNum,pageSize);
        return facilityDomain.findStorage(vo,lx);
    }

    //设备入库详情
    @Override
    public MachineVO findStorageById(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<DSbxxsjyVO> dSbxxsjyVOList = facilityDomain.findStorageById(vo);
        PageInfo<DSbxxsjyVO> pageInfos = new PageInfo<DSbxxsjyVO>(dSbxxsjyVOList);
        MachineVO machineVO = new MachineVO();
        if (dSbxxsjyVOList!=null){
            DSbxxsjyVO dSbxxsjyVO = dSbxxsjyVOList.get(0);
            machineVO.setQymc(dSbxxsjyVO.getQymc());
            machineVO.setFddbr(dSbxxsjyVO.getFddbr());
            machineVO.setBgdh(dSbxxsjyVO.getBgdh());
            machineVO.setKdmc(dSbxxsjyVO.getKdmc());
            machineVO.setKddz(dSbxxsjyVO.getKddz());
        }else {
            return null;
        }
        machineVO.setDSbxxsjyVOPageInfo(pageInfos);
        //machineVO.setSbxxsjyVOList(dSbxxsjyVOList);
        return machineVO;
    }

    //设备申购分页
    @Override
    public List<DSbsgxxVO> findPurchase(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize, Integer lx) {
        PageHelper.startPage(pageNum,pageSize);
        return facilityDomain.findPurchase(vo,lx);
    }

    //设备申购详情
    @Override
    public MachineVO findPurchaseById(DSbxxsjyQueryVO vo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<DSbsgxxVO> dSbsgxxVOList = facilityDomain.findPurchaseById(vo);
        PageInfo<DSbsgxxVO> pageInfos = new PageInfo<DSbsgxxVO>(dSbsgxxVOList);
        MachineVO machineVO = new MachineVO();
        if (dSbsgxxVOList!=null){
            DSbsgxxVO dSbsgxxVO = dSbsgxxVOList.get(0);
            machineVO.setQymc(dSbsgxxVO.getQymc());
            machineVO.setFddbr(dSbsgxxVO.getFddbr());
            machineVO.setBgdh(dSbsgxxVO.getBgdh());
            machineVO.setKdmc(dSbsgxxVO.getKdmc());
            machineVO.setKddz(dSbsgxxVO.getKddz());
        }else {
            return null;
        }
        machineVO.setDSbsgxxVOPageInfo(pageInfos);
        //machineVO.setDSbsgxxVOList(dSbsgxxVOList);
        return machineVO;
    }

    //机械设备统计分析
    @Override
    public List<DSbtjfxVO> findSbtjfx(DSbtjfxVO vo, Integer pageNum, Integer pageSize, Integer lx) {
        PageHelper.startPage(pageNum,pageSize);
        List<DSbtjfxVO> sbtjfx = facilityDomain.findSbtjfx(vo, lx);
        for (DSbtjfxVO dSbtjfxVO : sbtjfx) {
            String kdmc = dSbtjfxVO.getKdmc();
            String kddm = dSbtjfxVO.getKddm();
            /* String kddm = facilityDomain.findKddm(kdmc);*/
            String sbyqmc = dSbtjfxVO.getSbyqmc();
            String sbfl = dSbtjfxVO.getSbfl();
            Integer lysl = facilityDomain.findSblysl(kddm,sbyqmc,sbfl, lx);
            if (lysl!=null){
                dSbtjfxVO.setLysl(lysl.toString());
            }else {
                dSbtjfxVO.setLysl("0");
            }
            String tyshxydm = dSbtjfxVO.getTyshxydm();
            if (dSbtjfxVO.getBfsl()==null){
                dSbtjfxVO.setBfsl("0");
            }
            if (dSbtjfxVO.getWxsl()==null){
                dSbtjfxVO.setWxsl("0");
            }
            if (dSbtjfxVO.getRksl()==null){
                dSbtjfxVO.setRksl("0");
            }
            String qymc = facilityDomain.findQymc(tyshxydm);
            dSbtjfxVO.setQymc(qymc);
        }
        return sbtjfx;
    }


}
