package com.mvc.service.taizhang;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.TaskBatchRemoteDetailMapper;
import com.mvc.entity.TaskBatchRemoteDetail;
import com.mvc.entity.TaskBatchRemoteDetailExample;
import com.mvc.model.StationAccModel;
import com.mvc.model.TaskBatchRemoteDetailModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class TaskBatchRemoteDetailService {

    @Autowired(required = false)
    private TaskBatchRemoteDetailMapper taskBatchRemoteDetailMapper;

    @Autowired
    private TaskBatchRemoteService taskBatchRemoteService;

    @Autowired
    private StationAccService stationAccService;

    @Autowired
    private TTaskDownService tTaskDownService;

    public Map<String, Object> findAllTaskBatchRemoteDetail(Integer page, Integer rows) {
        Map<String, Object> resMap = new HashMap<>();
        Page<Object> pageObject = PageHelper.startPage(page, rows);
        List<TaskBatchRemoteDetail> TaskBatchRemoteDetails = taskBatchRemoteDetailMapper.selectByExample(null);
        List<TaskBatchRemoteDetailModel> resList = new ArrayList<>();
        for (TaskBatchRemoteDetail TaskBatchRemoteDetail : TaskBatchRemoteDetails) {
            TaskBatchRemoteDetailModel model = new TaskBatchRemoteDetailModel();
            BeanUtils.copyProperties(TaskBatchRemoteDetail, model);
            resList.add(model);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    /**
     * 根据taskBatchId(父表业务主键)获取明细列表
     *
     * @param taskBatchId
     * @return
     */
    public List<TaskBatchRemoteDetailModel> findTaskBatchRemoteDetailByTaskBatchId(String taskBatchId) {
        TaskBatchRemoteDetailExample example = new TaskBatchRemoteDetailExample();
        TaskBatchRemoteDetailExample.Criteria criteria = example.createCriteria();
        criteria.andTaskBatchIdEqualTo(taskBatchId);

        List<TaskBatchRemoteDetail> TaskBatchRemoteDetails = taskBatchRemoteDetailMapper.selectByExample(example);
        List<TaskBatchRemoteDetailModel> resList = new ArrayList<>();
        for (TaskBatchRemoteDetail TaskBatchRemoteDetail : TaskBatchRemoteDetails) {
            TaskBatchRemoteDetailModel model = new TaskBatchRemoteDetailModel();
            BeanUtils.copyProperties(TaskBatchRemoteDetail, model);
            resList.add(model);
        }
        return resList;
    }

    public TaskBatchRemoteDetailModel saveTaskBatchRemoteDetail(TaskBatchRemoteDetailModel model) {
        model.setFinishedFlag("0");
        //关联出站点信息
        StationAccModel accModel = stationAccService.findStationAccByDeviceSn(model.getDeviceSn());
        if(accModel!=null){
            model.setStationCode(accModel.getStationCode());
            model.setStationName(accModel.getStationName());
        }
        TaskBatchRemoteDetail TaskBatchRemoteDetail = new TaskBatchRemoteDetail();
        BeanUtils.copyProperties(model, TaskBatchRemoteDetail);
        int insert = taskBatchRemoteDetailMapper.insert(TaskBatchRemoteDetail);
        model.setId(TaskBatchRemoteDetail.getId());
        return model;
    }

    public TaskBatchRemoteDetailModel updateTaskBatchRemoteDetail(TaskBatchRemoteDetailModel model) {
        TaskBatchRemoteDetail TaskBatchRemoteDetail = taskBatchRemoteDetailMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TaskBatchRemoteDetail);
        int i = taskBatchRemoteDetailMapper.updateByPrimaryKey(TaskBatchRemoteDetail);
        return model;
    }

    //删除TaskBatchRemoteDetail并删除相关的远程配置任务
    public int deleteTaskBatchRemoteDetail(int id) {
        TaskBatchRemoteDetail detail = taskBatchRemoteDetailMapper.selectByPrimaryKey(id);
        if(detail!=null){
            tTaskDownService.deleteTTaskDownByTaskid_Batch(detail.getTaskDownId());
        }
        int i = taskBatchRemoteDetailMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据taskBatchId删除明细(主表记录删除后,清空对应的子表记录)
     * @param taskBatchId
     * @return
     */
    public int deleteTaskBatchRemoteDetailByTaskBatchId(String taskBatchId) {
        TaskBatchRemoteDetailExample example = new TaskBatchRemoteDetailExample();
        TaskBatchRemoteDetailExample.Criteria criteria = example.createCriteria();
        criteria.andTaskBatchIdEqualTo(taskBatchId);

        return taskBatchRemoteDetailMapper.deleteByExample(example);
    }

    /**
     * 根据下行任务ID更新状态和对应的抄表器配置表ID
     * @param taskDownId
     * @param tAicbqCfgId
     */
    public void updateTaskBatchRemoteDetailByTaskDownId(String taskDownId,String tAicbqCfgId){
        TaskBatchRemoteDetailExample example = new TaskBatchRemoteDetailExample();
        TaskBatchRemoteDetailExample.Criteria criteria = example.createCriteria();
        criteria.andTaskDownIdEqualTo(taskDownId);
        List<TaskBatchRemoteDetail> taskBatchRemoteDetails = taskBatchRemoteDetailMapper.selectByExample(example);
        if(taskBatchRemoteDetails!=null&&taskBatchRemoteDetails.size()>0){
            TaskBatchRemoteDetail detail=taskBatchRemoteDetails.get(0);
            detail.setFinishedFlag("1");
            detail.setFinishedTime(new Date());
            detail.setAicqbCfgId(tAicbqCfgId);
            taskBatchRemoteDetailMapper.updateByPrimaryKey(detail);

            //刷新主表状态 2022-10-17 guozhao
            taskBatchRemoteService.updateFinishedStatus(detail.getTaskBatchId(),detail.getId());
        }
    }

    /**
     * 根据批量任务主表ID和SN获取明细Model
     * @param batchId
     * @param deviceSn
     * @return
     */
    public TaskBatchRemoteDetailModel findTaskBatchRemoteDetailByBatchIdAndDeviceSn(String batchId,String deviceSn){
        TaskBatchRemoteDetailExample example = new TaskBatchRemoteDetailExample();
        TaskBatchRemoteDetailExample.Criteria criteria = example.createCriteria();
        criteria.andTaskBatchIdEqualTo(batchId);
        criteria.andDeviceSnEqualTo(deviceSn);
        List<TaskBatchRemoteDetail> taskBatchRemoteDetails = taskBatchRemoteDetailMapper.selectByExample(example);
        if(taskBatchRemoteDetails!=null&&taskBatchRemoteDetails.size()>0){
            TaskBatchRemoteDetail detail=taskBatchRemoteDetails.get(0);
            TaskBatchRemoteDetailModel model=new TaskBatchRemoteDetailModel();
            BeanUtils.copyProperties(taskBatchRemoteDetails.get(0),model);
            return model;
        }
        return null;
    }
}
