package com.unissoft.webservice.impl;

import com.chinaitop.depot.intelligent.basicdata.mapper.BasicStorehouseMapper;
import com.chinaitop.depot.intelligent.basicdata.mapper.OrgInfoMapper;
import com.chinaitop.depot.intelligent.basicdata.model.BasicStorehouse;
import com.chinaitop.depot.intelligent.basicdata.model.BasicStorehouseExample;
import com.chinaitop.depot.intelligent.basicdata.model.OrgInfo;
import com.chinaitop.depot.intelligent.basicdata.model.OrgInfoExample;
import com.chinaitop.depot.intelligent.utils.DateUtils;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.VerificationSourceUtils;
import com.chinaitop.depot.intelligent.wsClient.WebServiceSer;
import com.chinaitop.depot.intelligent.wsClient.WebServiceSerSoap;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.unissoft.model.ResponseEntity;
import com.unissoft.model.SendEntity;
import com.unissoft.webservice.BasicDataSynchroService;
import org.apache.commons.lang.StringUtils;
import org.assertj.core.util.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

@Service
@SuppressWarnings("all")
public class BasicDataSynchroServiceImpl implements BasicDataSynchroService {
    final static Logger logger = LoggerFactory.getLogger(BasicDataSynchroServiceImpl.class);
    @Resource
    private OrgInfoMapper orgInfoMapper;

    @Resource
    private BasicStorehouseMapper basicStorehouseMapper;

    @Override
    public ResponseEntity saveGrainPointData(String dataSource, String orgId) {
        logger.info("传入验证值：" + dataSource + "    |库点code：" + orgId);
        if (!VerificationSourceUtils.verificationSource(dataSource)){
            return ResponseEntity.failed("验证失败！");
        }
        if (StringUtils.isNotBlank(orgId)){
            OrgInfoExample example = new OrgInfoExample();
            OrgInfoExample.Criteria criteria = example.createCriteria();
            criteria.andOrgIdEqualTo(Integer.valueOf(orgId));
            List<OrgInfo> orgInfos = orgInfoMapper.selectByExample(example);
            if (orgInfos != null){
                ArrayList<SendEntity> tGrainpoints = Lists.newArrayList();
                orgInfos.forEach(orgInfo -> {
                    // 封装推送数据
                    Map<String, Object> orgInfoMap = new HashMap<>();
                    orgInfoMap.put("vKdCode", orgInfo.getOrgId()); // 库点编码
                    orgInfoMap.put("vKdName", orgInfo.getOrgName()); // 库点名称
                    orgInfoMap.put("vKdShort", orgInfo.getShortName()); // 库点简称
                    orgInfoMap.put("vKdQyxz", orgInfo.getCompanyNature()); // 企业性质
                    orgInfoMap.put("vKdType", orgInfo.getBusiType()); // 企业类型
                    orgInfoMap.put("vBuildDate", DateUtils.formatDate(orgInfo.getCompleteDate() == null ? new Date() : orgInfo.getCompleteDate())); // 建造日期
                    orgInfoMap.put("fcapacity", orgInfo.getEffectiveCapacity()); // 容量
                    orgInfoMap.put("vTelephone", orgInfo.getContact()); // 电话
                    orgInfoMap.put("vFax", orgInfo.getFax()); // 传真
                    orgInfoMap.put("vKdaddress", orgInfo.getAddress()); // 地址
                    orgInfoMap.put("vPostCode", orgInfo.getPost()); // 邮件代码
                    orgInfoMap.put("vPerson", orgInfo.getLegalPerson());// 法人
                    orgInfoMap.put("fKdArea", orgInfo.getDepotArea()); // 库点面积
                    orgInfoMap.put("province", orgInfo.getProvince()); // 省
                    orgInfoMap.put("city", orgInfo.getCity()); // 市
                    orgInfoMap.put("province2", null); // 省2
                    tGrainpoints.add(SendEntity.insertConvert(orgInfoMap));
                });
                try {
                    // 创建json转换对象
                    ObjectMapper objectMapper = new ObjectMapper();
                    // 对象转json字符串
                    String jsonStr = objectMapper.writeValueAsString(tGrainpoints);
                    // webService对象
                    WebServiceSer webs = new WebServiceSer();
                    // 获取cxf代理
                    WebServiceSerSoap factor = webs.getWebServiceSerSoap();
                    // 推送数据并返回结果
                    String s = factor.saveGrainPointData(jsonStr);
                    ResponseEntity o = (ResponseEntity)JsonToObjectUtils.jsonToObject(s, ResponseEntity.class);
                    if (o.getState().equals("0")){
                        return ResponseEntity.ok(o.getMsg());
                    }else {
                        return ResponseEntity.failed(o.getMsg());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return ResponseEntity.failed("推送失败");
                }
            }
        }
        return null;
    }

    @Override
    public ResponseEntity saveWareHouseData(String dataSource, String orgId) {
        logger.info("传入验证值：" + dataSource + "    |库点code：" + orgId);
        if (!VerificationSourceUtils.verificationSource(dataSource)){
            return ResponseEntity.failed("验证失败！");
        }
        if (StringUtils.isNotBlank(orgId)){
            BasicStorehouseExample example = new BasicStorehouseExample();
            BasicStorehouseExample.Criteria criteria = example.createCriteria();
            criteria.andOrgIdEqualTo(Integer.valueOf(orgId));
            criteria.andDelFlagEqualTo(1);
            List<BasicStorehouse> basicStorehouses = basicStorehouseMapper.selectByExample(example);
            if (basicStorehouses != null){
                ArrayList<SendEntity> wareHouses = Lists.newArrayList();
                basicStorehouses.forEach(storehouse -> {
                    // 封装推送数据
                    Map<String, Object> wareHouseData = new HashMap<>();
                    wareHouseData.put("vKdCode", storehouse.getOrgId()); // 库点id
                    wareHouseData.put("vCfCode", storehouse.getStorehouseCode()); // 仓房编码
                    wareHouseData.put("vCfName", storehouse.getStorehouseName()); // 仓房名称
                    wareHouseData.put("vCfType", storehouse.getStorehouseType()); // 仓房类型编号
                    wareHouseData.put("vCfStruct", null); // 仓房结构
                    wareHouseData.put("vBuildType", storehouse.getArchitectureType()); // 建筑类型编号
                    wareHouseData.put("vCfLong", storehouse.getLength()); // 仓内长
                    wareHouseData.put("vCfWide", storehouse.getWidth()); // 仓内宽
                    wareHouseData.put("vCfHigh", storehouse.getHeigth()); // 仓内高
                    wareHouseData.put("fDesignCap", storehouse.getDesignCapacity()); // 设计仓容
                    wareHouseData.put("fActCap", null); //
                    wareHouseData.put("vWorkDate", DateUtils.formatDate(storehouse.getUsedate() == null ? new Date() : storehouse.getUsedate())); // 启用日期
                    wareHouseData.put("vCfStatus", null);
                    wareHouseData.put("vRemark", storehouse.getStorehouseState());
                    wareHouseData.put("vKeeperId", storehouse.getDutystoreman()); // 保管员
                    wareHouseData.put("duiliangH", storehouse.getGrainLineHeigth()); // 装粮线高
                    wareHouseData.put("isshuliangjiace", null); // 是否启用数量检测功能 1 启用
                    wareHouseData.put("isInCheck", null); // 是否在检查 1代表是 0或null代表不是
                    wareHouseData.put("checkMode", null); // 检查模式 标准或者快速
                    wareHouseData.put("isWzc", null);
                    wareHouseData.put("type", null);
                    wareHouseData.put("id", null);
                    wareHouses.add(SendEntity.insertConvert(wareHouseData));
                });
                try {
                    // 创建json转换对象
                    ObjectMapper objectMapper = new ObjectMapper();
                    // 对象转json字符串
                    String jsonStr = objectMapper.writeValueAsString(wareHouses);
                    // webService对象
                    WebServiceSer webs = new WebServiceSer();
                    // 获取cxf代理
                    WebServiceSerSoap factor = webs.getWebServiceSerSoap();
                    // 推送数据并返回结果
                    String s = factor.saveWareHouseData(jsonStr);
                    ResponseEntity o = (ResponseEntity)JsonToObjectUtils.jsonToObject(s, ResponseEntity.class);
                    if (o.getState().equals("0")){
                        return ResponseEntity.ok(o.getMsg());
                    }else {
                        return ResponseEntity.failed(o.getMsg());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return ResponseEntity.failed("推送失败");
                }
            }
        }
        return null;
    }
}
