package com.bohuikeji.delivery.backstage.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.TOrderArrears;
import com.bohuikeji.delivery.backstage.mapper.TOrderArrearsMapper;
import com.bohuikeji.delivery.backstage.service.ITOrderArrearsService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.component.RedisUtil;
import com.bohuikeji.frame.module.admin.utils.JsonUtil;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import com.bohuikeji.frame.module.admin.utils.YiXunGmsUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 欠费订单表 服务实现类
 * </p>
 *
 * @author sjh
 * @since 2022-03-15
 */
@RequiredArgsConstructor
@Service
public class TOrderArrearsServiceImpl extends ServiceImplBase<TOrderArrearsMapper, TOrderArrears> implements ITOrderArrearsService {
    final ITOrderService orderService;
    final RedisUtil redisUtil;

    @Override
    public TOrderArrears insertRecord(String orderNum, String phone, String gmsCode, BigDecimal price,String url) {
        TOrderArrears arrears=selectOneByColumn("order_num",orderNum);
        TOrder order= orderService.getByNum(orderNum);
        if (arrears==null){
          arrears=new TOrderArrears();
          arrears.setOrderNum(orderNum);
          arrears.setThirdNum(order.getThirdNum());
          arrears.setPlayerUser(order.getPlayerId());
          arrears.setPlayerPhone(order.getPlayerId());
          arrears.setSenderName(order.getMailingName());
          arrears.setSenderMobile(order.getMailingPhone());
          arrears.setSenderAddress(order.getMailingAddress());
          arrears.setPickTime(order.getConfirmPickTime());
          arrears.setReceiveName(order.getReceiptName());
          arrears.setReceiveMobile(order.getReceiptPhone());
          arrears.setReceiveAddress(order.getReceiptAddress());
          arrears.setCreatePrice(order.getOrderExpectPrice());
          arrears.setPaidPrice(order.getOrderFactPrice());
          arrears.setThirdPrice(order.getOrderFactPrice().add(price));
          arrears.setArrearsPrice(price);
          arrears.setPayUrl(url);
          arrears.setOrderStatus(order.getOrderId());
          arrears.setArrearsStatus("欠费");
          JSONArray array=new JSONArray();
          array.add(getRecord(phone,gmsCode));
          arrears.setCallRecord(array);
          save(arrears);
          return arrears;
        }
        JSONArray records=arrears.getCallRecord();
        records.add(getRecord(phone,gmsCode));
        updateById(arrears);
        return arrears;
    }

    @Override
    public TOrderArrears insertRecord(Integer id, String phone, String text) {
        TOrderArrears arrears=getById(id);
        if (arrears==null){
            return null;
        }
        arrears.getCallRecord().add(getRecord2(phone,text));
        updateById(arrears);
        return arrears;
    }


    /**
     * 创建记录数据
     * @param phone
     * @param gmsCode
     * @return
     */
    private JSONObject getRecord(String phone, String gmsCode){
        String content= (String) redisUtil.get(YiXunGmsUtils.getCacheKey(phone,gmsCode));
        Map<String,String> detail=new HashMap<>(4);
        detail.put("催缴时间", LocalDateTime.now().format(StringUtils.dataTimeFormat));
        detail.put("催缴方式","短信通知");
        detail.put("催缴详情","发送到["+phone+"]："+content);
        detail.put("备注","无");
        return JsonUtil.objToJson(detail);
    }
    private JSONObject getRecord2(String phone, String text){
        Map<String,String> detail=new HashMap<>(4);
        detail.put("催缴时间", LocalDateTime.now().format(StringUtils.dataTimeFormat));
        detail.put("催缴方式","人工发送短信");
        detail.put("催缴详情","发送到["+phone+"]："+text);
        detail.put("备注","无");
        return JsonUtil.objToJson(detail);
    }
}
