package me.zhengjie.config.rabbitmq.test;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.OrderItem;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.config.rabbitmq.cuisine.CuisineProduce;
import me.zhengjie.config.rabbitmq.order.OrderProduce;
import me.zhengjie.modules.dm.daypc.domain.DmDayPc;
import me.zhengjie.modules.dm.daypc.repository.DmDayPcRepository;
import me.zhengjie.modules.dm.daypc.service.DmDayPcService;
import me.zhengjie.modules.dm.food.domain.DmFood;
import me.zhengjie.modules.dm.food.repository.DmFoodRepository;
import me.zhengjie.modules.dm.food.service.DmFoodService;
import me.zhengjie.modules.dm.foodCate.domain.DmFoodCate;
import me.zhengjie.modules.dm.foodCate.repository.DmFoodCateRepository;
import me.zhengjie.modules.dm.order.domain.DmOrderItem;
import me.zhengjie.modules.dm.order.domain.DmOrderPayment;
import me.zhengjie.modules.dm.order.domain.DmOrderRecord;
import me.zhengjie.modules.dm.order.service.DmOrderItemService;
import me.zhengjie.modules.dm.order.service.DmOrderPaymentService;
import me.zhengjie.modules.dm.order.service.DmOrderRecordService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.UUID;

@Component
@Slf4j
public class ListenerTemplate {
    @Autowired
    DmDayPcRepository dmDayPcRepository;
    @Autowired
    DmFoodRepository dmFoodRepository;
    @Autowired
    DmFoodCateRepository dmFoodCateRepository;
//    @Autowired
//    DmOrderRecordService dmOrderRecordService;
//    @Autowired
//    DmOrderItemService dmOrderItemService;
//    @Autowired
//    DmOrderPaymentService dmOrderPaymentService;

    @RabbitListener(queues = CuisineProduce.NAME, containerFactory = CuisineProduce.LISTENER)
    public void dealDeclareMessage(Message message) throws UnsupportedEncodingException {
        String str = new String(message.getBody(),"utf-8");
        System.out.println("receivePc:"+str);
        JSONObject jsonObject = JSONObject.parseObject(str);
        Long seq = jsonObject.getLong("seq");
        System.out.println("pc-seq:"+seq);
        JSONArray array = jsonObject.getJSONArray("list");
        System.out.println("listData"+array);
        try {
            for (int i = 0; i < array.size(); i++) {
                Snowflake snowflake = IdUtil.createSnowflake(1, 1);
                JSONObject attendance = array.getJSONObject(i);
                DmDayPc dmDayPc = new DmDayPc();
                dmDayPc.setId(snowflake.nextId());
                dmDayPc.setCpid(attendance.getLong("id"));
                dmDayPc.setTimes(attendance.getString("times"));
                dmDayPc.setPrice(attendance.getBigDecimal("price"));
                dmDayPc.setQtyBook(attendance.getString("qty_book"));
                dmDayPc.setWindow(attendance.getInteger("window"));
                dmDayPc.setState(attendance.getString("state"));
                dmDayPc.setName(attendance.getString("name"));
                DmFoodCate cate = dmFoodCateRepository.findByCateId(attendance.getInteger("cate"));
                if (ObjectUtil.isNull(cate)){
                    continue;
                }
                cate.setCateId(attendance.getInteger("cate"));
                dmDayPc.setCate(cate);
                dmDayPc.setPid(attendance.getLong("pid"));
                dmDayPc.setGoods(attendance.getString("goods"));
                dmDayPc.setBarcode(attendance.getString("barcode"));
                dmDayPc.setNo(attendance.getString("no"));
                dmDayPc.setPinyin(attendance.getString("pinyin"));
                dmDayPc.setPstate(attendance.getInteger("pstate"));
                dmDayPc.setWindowName(attendance.getString("window_name"));
                dmDayPc.setCateName(attendance.getString("cate_name"));
                dmDayPc.setShopname("东信餐厅");
                dmDayPc.setSeq(seq);
                dmDayPc.setDate(new Timestamp(System.currentTimeMillis()));

                dmDayPcRepository.save(dmDayPc);
                System.out.println("新增dmDayPc===" + dmDayPc);
                DmFood dmFood = new DmFood();
                dmFood.setId(dmDayPc.getPid());
                dmFood.setName(dmDayPc.getName());
                dmFood.setPrice(dmDayPc.getPrice());
                dmFood.setType(dmDayPc.getCate());//.toString()
                dmFood.setCode(cate.getCateId().toString());
                dmFood.setPstate(dmDayPc.getPstate());
                dmFoodRepository.save(dmFood);
            }
        }catch (Exception ex){
            log.error("订阅信息失败错误原因：{}", ex.getMessage());
        }
    }

//    @RabbitListener(queues = OrderProduce.NAME_ORDER, containerFactory = OrderProduce.LISTENER_ORDER)
//    public void dealDeclareOrderMessage(Message message) throws UnsupportedEncodingException {
//        System.out.println("messageOrder"+message);
//        String str = new String(message.getBody(),"utf-8");
//        System.out.println("receiveOrder:"+str);
//        try {
//            JSONObject attendance = JSONObject.parseObject(str);
//            DmOrderRecord dmExpenseCalendar = new DmOrderRecord();
//
//            dmExpenseCalendar.setSeq(attendance.getLong("seq"));
//            dmExpenseCalendar.setCreateDate(attendance.getTimestamp("create_date"));
//            dmExpenseCalendar.setCard(attendance.getString("card"));
//            dmExpenseCalendar.setCardNo(attendance.getString("card_no"));
//            dmExpenseCalendar.setType(attendance.getInteger("type"));
//            dmExpenseCalendar.setPart(attendance.getInteger("part"));
//            dmExpenseCalendar.setTerm(attendance.getInteger("term"));
//            dmExpenseCalendar.setShop(attendance.getInteger("shop"));
//            dmExpenseCalendar.setTermName(attendance.getString("term_name"));
//            dmExpenseCalendar.setShopName(attendance.getString("shop_name"));
//            dmExpenseCalendar.setAmt(attendance.getDouble("amt"));
//            dmExpenseCalendar.setMoney(String.valueOf(attendance.getDouble("amt")));
//            dmExpenseCalendar.setBalance(attendance.getDouble("balance"));
//            dmExpenseCalendar.setAccount(attendance.getInteger("account"));
//            dmExpenseCalendar.setUserName(attendance.getString("name"));
//            dmExpenseCalendar.setMob(attendance.getString("mob"));
//            dmExpenseCalendar.setUserCode(attendance.getString("mob"));
//            dmExpenseCalendar.setTel(attendance.getString("tel"));
//            dmExpenseCalendar.setCardProfile(attendance.getInteger("card_profile"));
//
//            String bh = UUID.randomUUID().toString();
//            dmExpenseCalendar.setBh(bh);//关联编号
//            JSONArray item = attendance.getJSONArray("details");
//            for (int i = 0; i < item.size(); i++) {
//                JSONObject userData = item.getJSONObject(i);
//                DmOrderItem dmOrderItem = new DmOrderItem();
//                DmFood dmFood = new DmFood();
//                dmFood.setId(userData.getLong("pid"));
//                dmOrderItem.setSeq(attendance.getLong("seq"));
//                dmOrderItem.setFood(dmFood);
//                dmOrderItem.setBh(bh);
//                dmOrderItem.setName(userData.getString("name"));
//                dmOrderItem.setPrice(userData.getString("price"));
//                dmOrderItem.setAmt(userData.getDouble("amt"));
//                dmOrderItem.setMoney(String.valueOf(userData.getDouble("amt")));
//                dmOrderItemService.create(dmOrderItem);
//            }
//            JSONArray payment = attendance.getJSONArray("payments");
//            for (int i = 0; i < payment.size(); i++) {
//                JSONObject userData = payment.getJSONObject(i);
//                DmOrderPayment dmOrderPayment = new DmOrderPayment();
//                dmOrderPayment.setBh(bh);
//                dmOrderPayment.setType(userData.getString("type"));
//                dmOrderPayment.setAmt(userData.getDouble("amt"));
//                dmOrderPayment.setBalance(userData.getDouble("balance"));
//                dmOrderPayment.setUid(userData.getString("uid"));
//                dmOrderPaymentService.create(dmOrderPayment);
//            }
//            dmOrderRecordService.create(dmExpenseCalendar);
//        }catch (Exception ex){
//            ex.printStackTrace();
//        }
//    }

}
