package com.cweb.controller;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsStore;
import com.hfkj.entity.BsStoreCid;
import com.hfkj.model.ResponseData;
import com.hfkj.service.BsStoreCidService;
import com.hfkj.service.BsStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author sum1dream
 */
@Controller
@RequestMapping(value = "/storeCid")
@Api(value = "门店CID")
public class BsStoreCidController {

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

    @Resource
    private BsStoreCidService bsStoreCidService;

    @Resource
    private BsStoreService bsStoreService;

    @RequestMapping(value="/connectStoreCid",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "连接门店和CID")
    public ResponseData connectStoreCid(@RequestBody BsStoreCid storeCid) {
        try {

            if (storeCid == null
                    || StringUtils.isBlank(storeCid.getCid())
                    || storeCid.getStoreId() == null
            ) {
                log.error("configStore error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }


            BsStore store = bsStoreService.getStoreById(storeCid.getStoreId());

            if (store == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前门店不存在！");
            }

            Map<String , Object> map = new HashMap<>();
            map.put("storeId" , storeCid.getStoreId());

            BsStoreCid bsStoreCid = bsStoreCidService.findByMap(map);

            if (bsStoreCid == null) {
                bsStoreCid = new BsStoreCid();
                bsStoreCid.setCid(storeCid.getCid());
                bsStoreCid.setStoreId(store.getId());
                bsStoreCid.setStoreNo(store.getStoreNo());
                bsStoreCid.setStoreName(store.getName());
                bsStoreCid.setCreateTime(new Date());
                bsStoreCid.setUpdateTime(new Date());
                bsStoreCidService.insertStoreCid(bsStoreCid);
            } else {
                bsStoreCid.setCid(storeCid.getCid());
                bsStoreCid.setUpdateTime(new Date());
                bsStoreCidService.editStoreCid(bsStoreCid);
            }

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/voiceSucceed",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "语音播报成功")
    public ResponseData voiceSucceed(@RequestParam(value = "storeId", required = true) Long storeId,
                                     @RequestParam(value = "taskNo", required = true) String taskNo) {
        try {

            bsStoreCidService.voiceSucceed(storeId , taskNo);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }


}
