package com.mvc.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.TMaintenanceRecordsV2Mapper;
import com.mvc.entity.TMaintenanceRecordsV2;
import com.mvc.entity.TMaintenanceRecordsV2Example;
import com.mvc.model.ResPageModel;
import com.mvc.model.StationAccModel;
import com.mvc.model.TDeviceOperationModel;
import com.mvc.model.biz.TMaintenanceRecordsV2Model;
import com.mvc.model.TaskRemoteCfgModel;
import com.mvc.service.copy.TAireadService;
import com.mvc.service.taizhang.StationAccService;
import com.mvc.service.taizhang.TaskRemoteCfgService;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TMaintenanceRecordsV2Service {

    @Resource
    private StationAccService stationAccService;

    @Resource
    private TMaintenanceRecordsV2Mapper tMaintenanceRecordsV2Mapper;

    @Resource
    private TaskRemoteCfgService taskRemoteCfgService;

    @Resource
    private TDeviceOperationService deviceOperationService;

    @Resource
    private TAireadService aireadService;

    /**
     * 分页查询全部记录
     *
     * @param queryModel
     * @return
     */
    public ResPageModel findAllTMaintenanceRecordsV2(TMaintenanceRecordsV2Model queryModel) {
        TMaintenanceRecordsV2Example example = new TMaintenanceRecordsV2Example();
        TMaintenanceRecordsV2Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getStationCode())) {
            criteria.andStationCodeEqualTo(queryModel.getStationCode());
        }
        if (StringUtils.isNotEmpty(queryModel.getOldDeviceSn())) {
            criteria.andOldDeviceSnEqualTo(queryModel.getOldDeviceSn());
        }
        if (StringUtils.isNotEmpty(queryModel.getNewDeviceSn())) {
            criteria.andNewDeviceSnEqualTo(queryModel.getNewDeviceSn());
        }
        if (StringUtils.isNotEmpty(queryModel.getMaintenanceType())) {
            criteria.andMaintenanceTypeEqualTo(queryModel.getMaintenanceType());
        }
        if (StringUtils.isNotEmpty(queryModel.getMaintenanceMan())) {
            criteria.andMaintenanceManLike(StringUtil.getLikeFormat(queryModel.getMaintenanceMan()));
        }

        example.setOrderByClause(" id desc ");

        Page<Object> pageObject = PageHelper.startPage(queryModel.getPageNum(), queryModel.getPageSize());
        List<TMaintenanceRecordsV2> tMaintenanceRecordsV2s = tMaintenanceRecordsV2Mapper.selectByExample(example);
        List<TMaintenanceRecordsV2Model> resList = new ArrayList<>();
        for (TMaintenanceRecordsV2 tMaintenanceRecordsV2 : tMaintenanceRecordsV2s) {
            TMaintenanceRecordsV2Model model = new TMaintenanceRecordsV2Model();
            BeanUtils.copyProperties(tMaintenanceRecordsV2, model);
            resList.add(model);

            if (model.getOldDeviceCfgId() != null) {
                TaskRemoteCfgModel oldCfgModel = taskRemoteCfgService.findTaskRemoteCfgById(model.getOldDeviceCfgId());
                if (oldCfgModel != null) {
                    model.setOldDeviceCfgStatus(oldCfgModel.getFinishedFlag());
                }
            }

            if (model.getNewDeviceCfgId() != null) {
                TaskRemoteCfgModel newCfgModel = taskRemoteCfgService.findTaskRemoteCfgById(model.getNewDeviceCfgId());
                if (newCfgModel != null) {
                    model.setNewDeviceCfgStatus(newCfgModel.getFinishedFlag());
                }
            }

            StationAccModel stationAccModel = stationAccService.findStationAccByStationCode(model.getStationCode());
            if (stationAccModel != null) {
                model.setStationName(stationAccModel.getStationName());
            }
        }
        ResPageModel resPageModel = new ResPageModel(resList, pageObject);
        return resPageModel;
    }

    /**
     * 保存新增对象
     *
     * @param model
     * @return
     */
    public TMaintenanceRecordsV2Model saveTMaintenanceRecordsV2(TMaintenanceRecordsV2Model model) {
        TMaintenanceRecordsV2 tMaintenanceRecordsV2 = new TMaintenanceRecordsV2();
        BeanUtils.copyProperties(model, tMaintenanceRecordsV2);
        int insert = tMaintenanceRecordsV2Mapper.insert(tMaintenanceRecordsV2);
        model.setId(tMaintenanceRecordsV2.getId());

         aireadService.findLatesTAireadByStationCode(model.getStationCode());
        saveDeviceOperationModel(model);
        return model;
    }

    /**
     * 根据维护记录生成站点投运记录
     *
     * @param model
     */
    private void saveDeviceOperationModel(TMaintenanceRecordsV2Model model) {
        TDeviceOperationModel deviceOperationModel = new TDeviceOperationModel(model);
        deviceOperationService.saveTDeviceOperation(deviceOperationModel);
    }

    /**
     * 修改对象
     *
     * @param model
     * @return
     */
    public TMaintenanceRecordsV2Model updateTMaintenanceRecordsV2(TMaintenanceRecordsV2Model model) {
        TMaintenanceRecordsV2 tMaintenanceRecordsV2 = tMaintenanceRecordsV2Mapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, tMaintenanceRecordsV2);
        int i = tMaintenanceRecordsV2Mapper.updateByPrimaryKey(tMaintenanceRecordsV2);
        return model;
    }

    /**
     * 删除对象
     *
     * @param id
     * @return
     */
    public int deleteTMaintenanceRecordsV2(int id) {
        int i = tMaintenanceRecordsV2Mapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据ID获取对象
     *
     * @param id
     * @return
     */
    public TMaintenanceRecordsV2Model findTMaintenanceRecordsV2ById(int id) {
        TMaintenanceRecordsV2Model model = null;
        TMaintenanceRecordsV2 tMaintenanceRecordsV2 = tMaintenanceRecordsV2Mapper.selectByPrimaryKey(id);
        if (tMaintenanceRecordsV2 != null) {
            model = new TMaintenanceRecordsV2Model();
            BeanUtils.copyProperties(tMaintenanceRecordsV2, model);
        }
        return model;
    }
}
