package com.mvc.service.copy;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.TAireadMapper;
import com.mvc.dao.TCheckDetailMapper;
import com.mvc.entity.*;
import com.mvc.model.*;
import com.mvc.service.dic.DicChkplatCellService;
import com.mvc.service.dic.DicChkplatService;
import com.mvc.utils.DateUtil;
import com.mvc.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class TCheckDetailService {

    @Resource
    private TCheckDetailMapper tCheckDetailMapper;

    @Resource
    private TAireadMapper tAireadMapper;

    @Resource
    private DicChkplatService dicChkplatService;

    @Resource
    private DicChkplatCellService dicChkplatCellService;

    @Resource
    private TCheckService tCheckService;

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

    public TCheckDetailModel saveTCheckDetail(TCheckDetailModel model) {
        TCheckDetail TCheckDetail = new TCheckDetail();
        BeanUtils.copyProperties(model,TCheckDetail);
        int insert = tCheckDetailMapper.insert(TCheckDetail);
        model.setId(TCheckDetail.getId());
        return model;
    }

    public TCheckDetailModel updateTCheckDetail(TCheckDetailModel model) {
        TCheckDetail TCheckDetail = tCheckDetailMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model,TCheckDetail);
        int i = tCheckDetailMapper.updateByPrimaryKey(TCheckDetail);
        return model;
    }

    public int deleteTCheckDetail(int id) {
        int i = tCheckDetailMapper.deleteByPrimaryKey(id);
        return i;
    }
    //根据拷机id 查找明细方法
    public List<TCheckDetailModel> findDetailsByCheckId(String checkId) {
        List<TCheckDetailModel> resModel=new ArrayList<>();
        TCheckDetailExample example=new TCheckDetailExample();
        TCheckDetailExample.Criteria criteria = example.createCriteria();
        criteria.andCheckIdEqualTo(checkId);

        example.setOrderByClause("DEVICE_SN");
        List<TCheckDetail> detailList = tCheckDetailMapper.selectByExample(example);
        for(TCheckDetail detail:detailList){
            TCheckDetailModel model = new TCheckDetailModel();
            BeanUtils.copyProperties(detail,model);
            if(model.getChkpaltId()!=null){
                DicChkplatModel dicChkplatModel = dicChkplatService.findDicChkplatByPlatId(model.getChkpaltId());
                if(dicChkplatModel!=null){
                    model.setPlatName(dicChkplatModel.getPlatName());
                }
            }
            if(model.getCellId()!=null){
                DicChkplatCellModel cellModel = dicChkplatCellService.findCellModelbycellId(model.getCellId());
                if(cellModel!=null){
                    model.setCellName(cellModel.getCellName());
                }
            }
            resModel.add(model);
        }
        return resModel;
    }
    //根据设备sn,拷机开始时间，结束时间查询拷机数据
    public List<TAireadModel> findCopyDatasBySn(String deviceSn,String startTime,String endTime) {
        List<TAireadModel> resModels = new ArrayList<>();
        Date startdate = DateUtil.parseTimeStr(startTime, "");
        Date enddate= DateUtil.parseTimeStr(endTime,"");
        TAireadExample example=new TAireadExample();
        TAireadExample.Criteria criteria = example.createCriteria();
        criteria.andDeviceSnEqualTo(deviceSn);
        criteria.andSampleTimeGreaterThanOrEqualTo(startdate);
        criteria.andSampleTimeLessThanOrEqualTo(enddate);
        List<TAiread> tAireads = tAireadMapper.selectByExample(example);
        for(TAiread airead:tAireads){
            TAireadModel model = new TAireadModel();
            BeanUtils.copyProperties(airead,model);
            resModels.add(model);
        }
        return resModels;
    }

    /**
     * 根据主键获取对象
     * @param id
     * @return
     */
    public TCheckDetail findTCheckDetailById(int id) {
        TCheckDetail TCheckDetail = tCheckDetailMapper.selectByPrimaryKey(id);
        return TCheckDetail;
    }

    //
    public TCheckDetailModel addTCheckDetail(TCheckDetailModel mxmodel) {
        TCheckModel theckModel = tCheckService.findBycheckByIdWithOutMx(mxmodel.getCheckId());
        mxmodel.setDetailId(StringUtil.getUuid());
        mxmodel.setStartTime(theckModel.getStartTime());
        mxmodel.setEndTime(theckModel.getEndTime());
        mxmodel.setCheckDays(theckModel.getCheckDays());
        mxmodel.setCheckIntverl(theckModel.getCheckIntverl());
       //查找工位，设置静态标识和标准值
        if(mxmodel.getCellId()!=null){
            DicChkplatCellModel cellModel = dicChkplatCellService.findCellModelbycellId(mxmodel.getCellId());
            mxmodel.setCellStaticFlag(cellModel.getStaticFlag());
            mxmodel.setStdValue(cellModel.getStdValue());
        }
        //
        TCheckDetail detail = new TCheckDetail();
        BeanUtils.copyProperties(mxmodel,detail);
        tCheckDetailMapper.insert(detail);
        mxmodel.setId(detail.getId());
        return mxmodel;
    }
}
