package com.ruoyi.web.controller.coffee;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.Machine;
import com.ruoyi.system.domain.vo.PushBean;
import com.ruoyi.system.jiguang.impl.JiGuangPushServiceImpl;
import com.ruoyi.system.service.impl.MachineServiceImpl;
import com.ruoyi.system.service.impl.OrderServiceImpl;
import com.ruoyi.system.service.impl.OrderTakingServiceImpl;
import com.ruoyi.system.service.impl.ShopServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/application")
public class ApplicationController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MachineServiceImpl machineService;
    @Autowired
    JiGuangPushServiceImpl jiGuangPushService;
    @Autowired
    private OrderTakingServiceImpl orderTakingService;
    @Autowired
    private ShopServiceImpl shopService;
    @Autowired
    private OrderServiceImpl orderService;
    @RequestMapping("/saveData")
    public AjaxResult saveApplicationData(@RequestParam("machineCode")String machineCode, @RequestBody String body){
        String id = UUID.randomUUID().toString();
        String key = machineCode+"_"+id;
        stringRedisTemplate.opsForValue().set(key, body);
        stringRedisTemplate.expire(id,1, TimeUnit.HOURS);
        return AjaxResult.success("处理成功",key);
    }
    @RequestMapping("/getData")
    public AjaxResult getApplicationData(String key){
        String s = stringRedisTemplate.opsForValue().get(key);
        stringRedisTemplate.delete(key);
        String[] keys = key.split("_");
        //推送数据给安卓
        PushBean pushBean = new PushBean();
        Map<String,String> map = new HashMap();
        map.put("type","1");
        map.put("code","200");
        jiGuangPushService.push(keys[0],map);
        return AjaxResult.success("处理成功",s);
    }
    @RequestMapping("/jgRegister")
    public AjaxResult jgRegister(@RequestParam String machineCode,@RequestParam String registerId){


        Machine machine = machineService.selectMachineById(machineCode);
        if(machine==null ){
            throw new ServiceException("该机器未绑定店铺，请先绑定店铺");
        }

        machine.setRegister(registerId);
        return AjaxResult.success(machineService.updateMachine(machine));
    }
    @RequestMapping("/getOrderTaking")
    public AjaxResult getOrderTaking(String machineCode){
        return AjaxResult.success(orderTakingService.getMachineInfo(machineCode));
    }

    @RequestMapping("/getOrderInfo")
    public AjaxResult getOrderInfo(String machineCode){
        return AjaxResult.success(orderService.getOrderInfo(machineCode));
    }
    @RequestMapping("/checkSku")
    private AjaxResult checkSku(@RequestParam(required = false) String shopId,String skuId,@RequestParam(required = false) String machineCode){
        return shopService.checkSku(shopId,skuId,machineCode);
    }
}
