package com.steerinfo.meterwork.meterworkcalibrationmain.service.impl;

import com.steerinfo.baseinfo.meterbasecalibrarelation.mapper.MeterBaseCalibraRelationMapper;
import com.steerinfo.baseinfo.meterbasecalibrarelation.model.MeterBaseCalibraRelation;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.meterwork.dto.CombinationList;
import com.steerinfo.meterwork.meterworkcalibrationbranch.mapper.MeterWorkCalibrationBranchMapper;
import com.steerinfo.meterwork.meterworkcalibrationbranch.model.MeterWorkCalibrationBranch;
import com.steerinfo.meterwork.meterworkcalibrationmain.mapper.MeterWorkCalibrationMainMapper;
import com.steerinfo.meterwork.meterworkcalibrationmain.model.MeterWorkCalibrationMain;
import com.steerinfo.meterwork.meterworkcalibrationmain.service.IMeterWorkCalibrationMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * MeterWorkCalibrationMain服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2020-09-25 02:57
 * 类描述
 * 修订历史：
 * 日期：2020-09-25
 * 作者：generator
 * 参考：
 * 描述：MeterWorkCalibrationMain服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "meterWorkCalibrationMainService")
public class MeterWorkCalibrationMainServiceImpl extends BaseServiceImpl<MeterWorkCalibrationMain, String> implements IMeterWorkCalibrationMainService {

    @Autowired
    private MeterWorkCalibrationMainMapper meterWorkCalibrationMainMapper;

    @Resource
    private MeterWorkCalibrationBranchMapper meterWorkCalibrationBranchMapper;

    @Autowired
    private MeterBaseCalibraRelationMapper meterBaseCalibraRelationMapper;

    @Override
    protected IBaseMapper<MeterWorkCalibrationMain, String> getMapper() {
        return meterWorkCalibrationMainMapper;
    }

    @Override
    public RESTfulResult doQueryWf(MeterWorkCalibrationMain main){
        RESTfulResult rm=new RESTfulResult();
        rm.setCode("200");
        try{
            Map<String,Object> map= new HashMap<>();
            map.put("baseSpotNo",main.getBaseSpotNo());
            map.put("string","and VALUE_FLAG in ('1','2')  order by calibration_main_time desc");
            PageHelper.startPage(1, 10);
            List<MeterWorkCalibrationMain> rows = meterWorkCalibrationMainMapper.selectByParameters(map);
            PageList pageInfo = new PageList(rows);
            rm.setData(pageInfo);
        }catch(Exception ex){
            rm.setCode("500");
            rm.setMessage("服务端查询异常："+ex.getMessage());
        }
        return rm;
    }

    @Override
    public List<MeterBaseCalibraRelation> doQueryCalibrationWf(MeterWorkCalibrationMain main) {
        //查询已经完成的校秤类型
        List<MeterBaseCalibraRelation> list1 = meterBaseCalibraRelationMapper.doQueryCalibrationWf(main.getBaseSpotNo());
        List<String> limsStr = new ArrayList<>();
        if(list1 != null && list1.size() > 0){
            for (MeterBaseCalibraRelation obj : list1){
                limsStr.add(obj.getId());
            }
        }
        //查询需要完成的校秤类型
        List<MeterBaseCalibraRelation> list2 = meterBaseCalibraRelationMapper.doQueryCalibrationNeedWf(main.getBaseSpotNo());
        //两者同时有值的情况进行以下操作
        List<MeterBaseCalibraRelation> list3 = null;
        if (list1.size() >= 0 && list2.size() >= 0){
            list3 = new ArrayList<>();
            for(MeterBaseCalibraRelation obj : list2){
                if(limsStr.contains(obj.getId())){
                    list3.add(obj);
                }
            }
        }
        list2.removeAll(list3);
        return list2;
    }

    @Override
    public  CombinationList doQuerySomeWf(String baseSpotNo, int pageSize){
        CombinationList cl = new CombinationList();
        //主表数据
        Map<String,Object> map= new HashMap<>();
        map.put("baseSpotNo",baseSpotNo);
        map.put("pageSize",pageSize);
        List<MeterWorkCalibrationMain> list1 = meterWorkCalibrationMainMapper.doQuerySomeWf(map);
        cl.one = list1;

        List<String> ids = new ArrayList<>();
        for (MeterWorkCalibrationMain list : list1){
            ids.add(list.getCalibrationMainNo());
        }
        //次表数据
        List<MeterWorkCalibrationBranch> list2 = meterWorkCalibrationBranchMapper.doQuerySomeWf(ids);
        cl.two = list2;

        return cl;
    }

}
