package com.hfkj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.BsStoreCidMapper;
import com.hfkj.entity.BsStoreCid;
import com.hfkj.entity.BsStoreCidExample;
import com.hfkj.service.BsStoreCidService;
import com.hfkj.tts.HWYunSisService;
import com.hfkj.unipush.UniPushService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

/**
 * @author sum1dream
 */
@Service("bsStoreCidService")
public class BsStoreCidServiceImpl implements BsStoreCidService {

    private static Logger log = LoggerFactory.getLogger(BsStoreCidService.class);

    @Resource
    private BsStoreCidMapper bsStoreCidMapper;

    @Override
    public void insertStoreCid(BsStoreCid storeCid) {
        bsStoreCidMapper.insert(storeCid);
    }

    @Override
    public void editStoreCid(BsStoreCid storeCid) {
        bsStoreCidMapper.updateByPrimaryKey(storeCid);
    }

    @Override
    public BsStoreCid findByMap(Map<String, Object> map) {

        BsStoreCidExample example = new BsStoreCidExample();
        BsStoreCidExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(map, "storeId") != null) {
            criteria.andStoreIdEqualTo(MapUtils.getLong(map, "storeId"));
        }

        if (MapUtils.getLong(map, "storeNo") != null) {
            criteria.andStoreNoEqualTo(MapUtils.getLong(map, "storeNo"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "storeName"))) {
            criteria.andStoreNameLike("%"+MapUtils.getString(map, "storeName")+"%");
        }

        List<BsStoreCid> list = bsStoreCidMapper.selectByExample(example);

        if (list.size() > 0) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public void storePush(JSONObject object) {

        try {
            if (object == null ||
                    object.getBigDecimal("price") == null ||
                    object.getLong("storeId") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            Map<String, Object> map = new HashMap<>();
            map.put("storeId", object.getLong("storeId"));
            BsStoreCid bsStoreCid = findByMap(map);

            if (bsStoreCid == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "不存在推送关系");
            }
            // 获取语音播报地址
            String auditPath = HWYunSisService.ttsCustomDemo(object);

            if (auditPath == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "生成语音失败");
            }

            object.put("cid", bsStoreCid.getCid());
            object.put("body", auditPath);
            UniPushService.pushToSingleByCid(object);
        } catch (Exception e) {
            log.info("请求超时：" + e);
        }

    }

}
