package com.mortals.xhx.module.api.handler.impl;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.util.StringUtils;
import com.mortals.framework.util.ThreadPool;
import com.mortals.xhx.module.api.feign.boss.BossOperMainReq;
import com.mortals.xhx.module.api.feign.boss.IBossFeign;
import com.mortals.xhx.module.api.feign.boss.WorkType;
import com.mortals.xhx.module.api.model.AssetEventLog;
import com.mortals.xhx.module.api.model.AssetEventLogSubmitReq;
import com.mortals.xhx.module.asset.model.AssetEntity;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 *
 * boss 台账提交处理
 * @author luoshichao
 * @Description
 * @date 2021/3/11 16:11
 */
@Component
public class BossOpermainService {

    protected Log log = LogFactory.getLog(this.getClass());

//    @Autowired
//    private IBossFeign bossFeign;

    @Value("${cookie.port}")
    private Integer port;

    @Value("${cookie.domain}")
    private String domain;

    /**
     * 提交台账
     * @param req
     * @param assetEntity
     * @param workType
     */
    public void submitOpermain(AssetEventLog req, AssetEntity assetEntity, WorkType workType){
        AssetEventLogSubmitReq submitReq = (AssetEventLogSubmitReq)req;
        BossOperMainReq submitOpermainParam = buildBossParam(submitReq, assetEntity, workType);
        ThreadPool.getInstance().init(8);
        ThreadPool.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                log.info("提交台账请求参数：" +  JSONObject.toJSONString(submitOpermainParam));
               // String result = bossFeign.submitOpermain(submitOpermainParam);
                log.info("提交台账返回结果：" );
            }
        });
    }

    /**
     * 构造提交参数
     * @param submitReq
     * @param assetEntity
     * @return
     */
    private BossOperMainReq buildBossParam(AssetEventLogSubmitReq submitReq, AssetEntity assetEntity, WorkType workType){
        JSONObject extInfo = new JSONObject(submitReq.getEventExtInfo());

        BossOperMainReq bossOperMainReq = new BossOperMainReq();
        bossOperMainReq.setStaffId(submitReq.getStaffId());
       // bossOperMainReq.setStaffName(submitReq.getStaffName());

        //因为运维助手的用户ID和boss中的用户ID可能不相等，所以用loginName标识操作人员，boss根据loginName查询对应的ID和name
        bossOperMainReq.setStaffName(submitReq.getEventUser());

        bossOperMainReq.setWorkType(workType.getValue());
        bossOperMainReq.setTogether(submitReq.getTogether());
        bossOperMainReq.setStartTime(submitReq.getEventStartTime());
        bossOperMainReq.setEndTime(submitReq.getEventEndTime());
        bossOperMainReq.setLongitude(new BigDecimal(submitReq.getLongitude()));
        bossOperMainReq.setLatitude(new BigDecimal(submitReq.getLatitude()));
        bossOperMainReq.setDeviceNum(assetEntity.getDeviceNum());
        //设备类型取业务码前两位
        bossOperMainReq.setProdType(Integer.parseInt((StringUtils.isNotEmpty(assetEntity.getDeviceNum()) ? assetEntity.getDeviceNum(): assetEntity.getAssetNum() ).substring(0,2)));
        //图片转换成完整地址给BOSS
        if(StringUtils.isNotEmpty(submitReq.getEventImgUrl()) && !submitReq.getEventImgUrl().equals("无")){
            String eventImg = "";

            String[] imgArr = submitReq.getEventImgUrl().split(",");
            for(String url: imgArr){
                String prefix = "";
                if(!url.startsWith("http")){
                    prefix = "http://" + prefix;
                }
                if(StringUtils.isNotEmpty(domain)){
                    prefix = prefix + domain;
                }
                if(port != null){
                    prefix = prefix + ":" + port;
                }
                url = prefix + url ;

                eventImg = eventImg+ "," + url ;
            }
            bossOperMainReq.setImgs(eventImg.startsWith(",")? eventImg.substring(1, eventImg.length()) : eventImg );
        }

        bossOperMainReq.setAreaId(submitReq.getAreaId());
        bossOperMainReq.setAreaName(submitReq.getAreaName());
        //bossOperMainReq.setShopId(submitReq.getShopId());
        /*if(workType.getValue() == WorkType.INSTALL.getValue()){
            bossOperMainReq.setReason(submitReq.getRemark());
            bossOperMainReq.setResult(submitReq.getRemark());
        }else if(workType.getValue() == WorkType.REMOVE.getValue()){
            bossOperMainReq.setReason(submitReq.getRemark());
            bossOperMainReq.setResult(submitReq.getRemark());
        }else if(workType.getValue() == WorkType.INSTALL.getValue()){
            bossOperMainReq.setReason(submitReq.getRemark());
            bossOperMainReq.setResult(submitReq.getRemark());
        }else if(workType.getValue() == WorkType.MAINTAIN.getValue()){
            String exRemark = extInfo.getString("exRemark");
            bossOperMainReq.setReason(exRemark);
        }*/

        if(workType.getValue() == WorkType.MAINTAIN.getValue()){
            String exRemark = extInfo.getString("exRemark");
            bossOperMainReq.setReason(exRemark);
        }
        if(extInfo.getLong("shopId") != null){
            bossOperMainReq.setShopId(extInfo.getLong("shopId"));
        }

        return bossOperMainReq;
    }

}
