package com.mvc.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.constant.BaseConstant;
import com.mvc.dao.TMarkRecordMapper;
import com.mvc.entity.TMarkRecord;
import com.mvc.entity.TMarkRecordExample;
import com.mvc.model.*;
import com.mvc.service.copy.TAireadService;
import com.mvc.service.dic.DicMarkTypeService;
import com.mvc.service.taizhang.StationAccService;
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.Date;
import java.util.List;

@Service
@Transactional
public class TMarkRecordService {
    @Resource
    private TMarkRecordMapper tMarkRecordMapper;

    @Resource
    private DicMarkTypeService dicMarkTypeService;

    @Resource
    private StationAccService stationAccService;

    @Resource
    private TAireadService tAireadService;

    public ResPageModel findAllTMarkRecord(TMarkRecordModel queryModel) {
        TMarkRecordExample example=new TMarkRecordExample();
        TMarkRecordExample.Criteria criteria=example.createCriteria();
        if(StringUtils.isNotEmpty(queryModel.getDeviceSn())){
            if (queryModel.getDeviceSn().length() == BaseConstant.LENGTH_DEVICE_SN) {
                criteria.andDeviceSnEqualTo(queryModel.getDeviceSn());
            } else {
                criteria.andDeviceSnLike(StringUtil.getLikeFormat(queryModel.getDeviceSn()));
            }
        }
        if(StringUtils.isNotEmpty(queryModel.getStationCode())){
            if (queryModel.getStationCode().length() == BaseConstant.LENGTH_STATION_CODE) {
                criteria.andStationCodeEqualTo(queryModel.getStationCode());
            } else {
                criteria.andStationCodeLike(StringUtil.getLikeFormat(queryModel.getStationCode()));
            }
        }
        if(queryModel.getMarkId()!=null){
            criteria.andMarkIdEqualTo(queryModel.getMarkId());
        }
        if(StringUtils.isNotEmpty(queryModel.getMarkDescription())){
            criteria.andMarkDescriptionLike(StringUtil.getLikeFormat(queryModel.getMarkDescription()));
        }
        if(StringUtils.isNotEmpty(queryModel.getProcessResults())){
            criteria.andProcessResultsEqualTo(queryModel.getProcessResults());
        }
        example.setOrderByClause("CREATE_TIME DESC");
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPage(), queryModel.getRows());
        List<TMarkRecordModel> resList=selectByExample(example);
        ResPageModel pageModel = new ResPageModel(resList, pageObject);
        return pageModel;
    }

    private List<TMarkRecordModel> selectByExample(TMarkRecordExample example){
        List<TMarkRecord> TMarkRecords = tMarkRecordMapper.selectByExample(example);
        List<TMarkRecordModel> resList = new ArrayList<>();
        for (TMarkRecord TMarkRecord : TMarkRecords) {
            TMarkRecordModel model = new TMarkRecordModel();
            BeanUtils.copyProperties(TMarkRecord, model);
            setMarkInfo(model);
            setLastRecordInfo(model);
            resList.add(model);
        }

        return resList;
    }

    /**
     * 赋值最新一条数据的信息
     * @param model
     */
    private void setLastRecordInfo(TMarkRecordModel model){
        TAireadModel aireadModel = tAireadService.findLatesTAireadByStationCode(model.getStationCode());
        if(aireadModel!=null){
            model.setLastSampleTime(aireadModel.getSampleTime());
            model.setLastSmallJpgUrl(aireadModel.getSmallJpgUrl());
        }
    }

    /**
     * 赋值标记信息
     * @param model
     */
    private void setMarkInfo(TMarkRecordModel model){
        DicMarkTypeModel typeModel = dicMarkTypeService.findDicMarkTypeModelById(model.getMarkId());
        if(typeModel!=null){
            model.setMarkTypeName(typeModel.getMarkName());
            DicMarkTypeModel parentModel = dicMarkTypeService.findDicMarkTypeModelById(typeModel.getParentId());
            if(parentModel!=null){//理论上不会为空
                model.setQueryParentId(parentModel.getId());
                model.setMarkParentName(parentModel.getMarkName());
            }
        }
    }

    public TMarkRecordModel saveTMarkRecord(TMarkRecordModel model) {
        model.setProcessResults("0");
        if(StringUtils.isEmpty(model.getReviewFlag())){
            model.setReviewFlag("0");
        }
        StationAccModel accModel = stationAccService.findStationAccByStationCode(model.getStationCode());
        if(accModel!=null){
            model.setStationName(accModel.getStationName());
            model.setDeviceSn(accModel.getDeviceSn());
        }
        model.setCreateTime(new Date());
        TMarkRecord TMarkRecord = new TMarkRecord();
        BeanUtils.copyProperties(model, TMarkRecord);
        int insert = tMarkRecordMapper.insert(TMarkRecord);
        model.setId(TMarkRecord.getId());
        return model;
    }

    public TMarkRecordModel updateTMarkRecord(TMarkRecordModel model) {
        TMarkRecord tMarkRecord = tMarkRecordMapper.selectByPrimaryKey(model.getId());
        if(tMarkRecord!=null){
            //当处理标记为空或标记不相同时更新处理时间2023-04-14 guozhao
            if(StringUtils.isEmpty(tMarkRecord.getProcessResults())||!model.getProcessResults().equals(tMarkRecord.getProcessResults())){
                model.setProcessTime(new Date());
            }
            BeanUtils.copyProperties(model, tMarkRecord);
            int i = tMarkRecordMapper.updateByPrimaryKey(tMarkRecord);
        }
        return model;
    }

    public int deleteTMarkRecord(int id) {
        int i = tMarkRecordMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 获取所有未处理的记录
     * @return
     */
    public List<TMarkRecordModel> findUntreatedModels(){
        TMarkRecordExample example=new TMarkRecordExample();
        TMarkRecordExample.Criteria criteria=example.createCriteria();
        criteria.andProcessResultsEqualTo("0");
        return selectByExample(example);
    }

    public TMarkRecordModel findUntreatedModelByStationCode(String stationCode){
        TMarkRecordExample example=new TMarkRecordExample();
        TMarkRecordExample.Criteria criteria=example.createCriteria();
        criteria.andStationCodeEqualTo(stationCode);
        criteria.andProcessResultsEqualTo("0");
        List<TMarkRecordModel> list = selectByExample(example);
        if(list!=null&&list.size()>0){
            TMarkRecordModel model=new TMarkRecordModel();
            BeanUtils.copyProperties(list.get(0),model);
            return model;
        }
        return null;
    }
}
