package com.steerinfo.server.serverimpl;


import com.alibaba.fastjson.JSON;

import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.baseinfo.combaseinfo.model.ComBaseInfo;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.meterwork.metertolimsold.mapper.MeterToLimsOldMapper;
import com.steerinfo.meterwork.metertolimsold.model.MeterToLimsOld;
import com.steerinfo.meterwork.meterworkstockonline.mapper.MeterWorkStockOnlineMapper;
import com.steerinfo.meterwork.meterworkstockonline.model.MeterWorkStockOnline;
import com.steerinfo.meterwork.meterworkstorageactual.mapper.MeterWorkStorageActualMapper;
import com.steerinfo.meterwork.meterworkstorageactual.model.MeterWorkStorageActual;
import com.steerinfo.server.WebServer;
import com.steerinfo.util.CommonPage;
import com.steerinfo.util.MapToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.text.SimpleDateFormat;
import java.util.*;


@WebService(serviceName = "WebServer",//对外发布的服务名
        targetNamespace = "http://server.subject.steerinfo.com",//指定命名空间,通常使用包名反转
        endpointInterface = "com.steerinfo.server.WebServer"//服务接口全路径,指定做SEI（Service EndPoint Interface）服务端点接口
)
@Component
public class WebServerimpl implements WebServer {

    @Resource
    private MeterToLimsOldMapper toLimsOldMapper;

    @Resource
    private MeterWorkStorageActualMapper storageActualMapper;

    @Resource
    private ComBaseInfoMapper baseInfoMapper;

    @Resource
    private MeterWorkStockOnlineMapper stockOnlineMapper;

    @Override
    public List<MeterToLimsOld> ExportAccess(CommonPage<MeterToLimsOld> list) {
        List<MeterToLimsOld> ljp=null;
        HashMap<String, Object> map = MapToolsUtil.objectToMap(list.getParam());
        map.put("startTime",list.getStartTime());
        map.put("endTime",list.getEndTime());
        ljp=toLimsOldMapper.QueryData(map);
        return ljp;
    }

    @Override
    public List<MeterWorkStorageActual> doQueryTimeWf(CommonPage<MeterWorkStorageActual> model) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("startTime", model.getStartTime());
        obj.put("endTime", model.getEndTime());
        if(model.getParam()!=null) {
            obj.put("baseSpotNo", model.getParam().getBaseSpotNo());
            obj.put("productName", model.getParam().getProductName());
        }
        return storageActualMapper.doQueryTimeWf(obj);
    }

    @Override
    public List<ComBaseInfo> doQueryComBaseInfoWf(ComBaseInfo cbi) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("pBaseCode", cbi.getpBaseCode());
        obj.put("validFlag", cbi.getValidFlag());
        return baseInfoMapper.selectByParameters(obj);
    }

    @Override
    public List<MeterWorkStockOnline> doQueryStockOnlineWf(MeterWorkStockOnline cbi) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("validFlag", cbi.getValidFlag());
        return stockOnlineMapper.selectByParameters(obj);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public RESTfulResult doAccessCuImpl(List<MeterWorkStorageActual> cbi) {
        RESTfulResult result = new RESTfulResult();
        boolean flag=true;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String no = storageActualMapper.getNewID(sdf.format(new Date()));
        int i=Integer.parseInt(no);  //123456789    6789      9-4
        no=sdf.format(new Date())+no;

        for (MeterWorkStorageActual online:cbi) {
            MeterWorkStorageActual mq=new MeterWorkStorageActual();
            mq.setBatchNo(online.getBatchNo());
            mq.setPackageNo(online.getPackageNo());
            int icnt=storageActualMapper.isHaveCntWf(mq);
            if(icnt==0) {
                online.setActualNo(no + String.format("%04d", i++));
                online.setCreateTime(new Date());
                storageActualMapper.insertSelective(online);
            }
        }

        result.setSucceed();
        result.setData("成功");
        
        return result;
    }

    @Override
    public RESTfulResult doUpdateProAct(List<MeterWorkStorageActual> cbi) {
        RESTfulResult result = new RESTfulResult();
        boolean flag=true;

        for (MeterWorkStorageActual online:cbi) {
            try {
                MeterWorkStorageActual msa=new MeterWorkStorageActual();
                msa.setUpdateManNo(online.getUpdateManNo());
                msa.setUpdateManName(online.getUpdateManName());
                msa.setValueFlag(online.getValueFlag());
                msa.setUpdateTime(new Date());
                storageActualMapper.updateByPrimaryKeySelective(msa);
            } catch (Exception ex) {
                flag = false;
                result.setMessage(ex.getMessage());
            }
        }

        if(flag){
            result.setSucceed();
            result.setData("成功");
        }else {
            result.setFailed();
            result.setData("失败");
        }
        return result;
    }

    @Override
    public List<MeterWorkStorageActual> doQueryProAct(CommonPage<MeterWorkStorageActual> model) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("startTime", model.getStartTime());
        obj.put("endTime", model.getEndTime());

        return storageActualMapper.doQueryNieWf(obj);
    }
}
