package com.bjtdba.merchant.controller;

import com.bjtdba.entity.merchant.MerchantYx;
import com.bjtdba.merchant.service.MerchantYxService;
import com.bjtdba.utils.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 *
 *
 * @author makejava
 * @since 2022-08-09 11:55:46
 */
@CrossOrigin
@RestController
@RequestMapping("/merchant_service/merchantYx")
public class MerchantYxController {
   private static final Logger loger= (Logger) LoggerFactory.getLogger(MerchantYxController.class);


    @Autowired
    private MerchantYxService merchantYxService;


    /**
     * 音响绑定功能
     *
     * @param map  param merId 商户id
     *      * @param uid 用户id
     *      * @param sn 音响号码
     *      * @return 新增结果
     */
    @PostMapping("/add")
    public Object  add(@RequestBody Map<String,Object> map) {
        if(!map.containsKey("merId")){
            return  ResponseUtil.fail();
        }
        if(!map.containsKey("uid")){
            return  ResponseUtil.fail();
        }
        int merId =Integer.parseInt(map.get("merId")+"");
        int uid =Integer.parseInt(map.get("uid")+"");
        String sn=map.get("sn")+"";
        loger.info("merchantYx/add request param:" +"merId"+merId+"uid:"+uid+"sn:"+sn);
        // 判断参数
        if (merId<=0){
            return ResponseUtil.fail();
        }
        if(uid<=0){
            return ResponseUtil.fail();
        }
        if("".equals(sn)){
            return ResponseUtil.fail();
        }
        // 参数没有问题进行执行
        MerchantYx myx=new MerchantYx();
        myx.setUid(uid);


        MerchantYx resMyx=new MerchantYx();

        MerchantYx yxObj=merchantYxService.queryByUid(uid);
        MerchantYx yxSn=merchantYxService.queryBySn(sn);
        if(yxSn!=null){
            loger.info("音响绑定失败,已经存在");
            return ResponseUtil.fail("音响已经绑定");
        }
        if(yxObj!=null){
            yxObj.setSn(sn);
            resMyx=merchantYxService.update(yxObj);
        }else{
            myx.setMerId(merId);
            myx.setTime(new Date());
            myx.setStatus(1);
            myx.setSn(sn);
            resMyx=this.merchantYxService.insert(myx);
        }


        if (resMyx!=null&&resMyx.getId()>0){
            loger.info("音响绑定成功");
            return ResponseUtil.ok();
        }else{
            loger.info("音响绑定失败");
            return ResponseUtil.fail();
        }



    }

    @PostMapping("/queryByMerId")
    public Object  queryByMerId(@RequestBody Map<String,Object> map){
        if(!map.containsKey("merId")){
            return  ResponseUtil.fail();
        }
        int merId=Integer.parseInt(map.get("merId")+"");
        MerchantYx merchantYx=merchantYxService.qeryByMerid(merId);
        if(merchantYx==null){
            loger.info("音响未绑定");
            return ResponseUtil.fail("音响未绑定");
        }else{
            return ResponseUtil.ok();
        }

    }


}

