package com.bjtdba.merchant.controller;

import com.bjtdba.entity.merchant.MerchantAdmin;
import com.bjtdba.merchant.config.ImConfig;
import com.bjtdba.merchant.service.MerchantAdminService;
import com.bjtdba.merchant.service.MerchantCashierService;
import com.bjtdba.utils.ResponseUtil;
import com.easemob.im.server.EMException;
import com.easemob.im.server.model.EMUser;
import io.netty.util.internal.ThreadLocalRandom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("merchant_service/cashierService")
public class MerchantCashierController {

    @Autowired
    private MerchantCashierService merchantCashierService;
    @Autowired
    private MerchantAdminService merchantAdminService;
    @Autowired
    private ImConfig imConfig;

    /**
     * 店铺信息  首款二维码哪里来
     */
    @PostMapping("/selectMerchant")
    public Object selectMerchant(@RequestBody Map<String,String> map) throws Exception {
        return merchantCashierService.selectMerchantByMerId(Integer.parseInt(map.get("mer_id")));
    }
    /**
     * 时间区间 收益情况  今天昨天，也是传两个时间
     */
    @PostMapping("/selectMerchantOrder")
    public Object selectMerchantOrder(@RequestBody Map<String,String> map){
        return merchantCashierService.selectMerchantOrder(Integer.parseInt(map.get("mer_id")),map.get("start_time"),map.get("end_time"));
    }
    /**
     * 最近支付用户
     */
    @PostMapping("/payUserList")
    public Object payUserList(@RequestBody Map<String,String> map){
        return merchantCashierService.payUserList(Integer.parseInt(map.get("pageNum")),Integer.parseInt(map.get("pageSize")),Integer.parseInt(map.get("mer_id")));
    }
    /**
     * 消费分
     * 1、判断该商户是否开启消费分
     * 2、以结算  pm=1 status =1
     * 3、预估   pm=1 status = 0
     * 4、以释放 pm=2
     */
    @PostMapping("/merchantConsumption")
    public Object merchantConsumption(@RequestBody Map<String,String> map){
        return merchantCashierService.merchantConsumption(Integer.parseInt(map.get("mer_id")));
    }

    /**
     * 开启消费分  判断商户设置的默认比例是否大于等于2%
     */

    @PostMapping("/updateConsumptionScore")
    public Object updateConsumptionScore(@RequestBody Map<String,String> map){
        return merchantCashierService.updateConsumptionScore(map.get("mer_id"));
    }
    /**
     * 获取消费分明细
     */
    @PostMapping("/selectMerchantConsumption")
    public Object selectMerchantConsumptionScoreLog(@RequestBody Map<String,String> map){
        return merchantCashierService.selectMerchantConsumptionScoreLog(Integer.parseInt(map.get("pageNum")),Integer.parseInt(map.get("pageSize")),map.get("mer_id"));
    }
    @PostMapping("/getImUser")
    public Object getImUser(@RequestBody Map<String,String> map){
        Integer uid= Integer.parseInt(map.get("mer_id"));
        MerchantAdmin merchantAdmin = merchantAdminService.queryMerchantAdmin(uid);
        Map<String,Object> data=new HashMap<String,Object>();

        if(merchantAdmin.getImUuid()==null || merchantAdmin.getImUuid().equals("")){
            // 没有im 去创建
            try {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < 6; i++) {
                    int digit = ThreadLocalRandom.current().nextInt(0, 10);
                    sb.append(digit);
                }
                int imPass = Integer.parseInt(sb.toString());
                System.out.println(imPass);

                merchantAdmin.setImUserId("mer"+merchantAdmin.getUid());
                merchantAdmin.setImPass(imPass+"");
                EMUser user = imConfig.service().user().create(merchantAdmin.getImUserId(), merchantAdmin.getImPass()).block();
                assert user != null;
                merchantAdmin.setImUuid(user.getUuid());
                merchantAdminService.updateMerchantAdmin(merchantAdmin);
            } catch (EMException e) {
                e.getErrorCode();
                e.getMessage();
            }
        }
        data.put("im_uuid",merchantAdmin.getImUuid());
        data.put("im_userid", merchantAdmin.getImUserId());
        data.put("im_pass", merchantAdmin.getImPass());
        return ResponseUtil.ok(data);
    }
}
