package com.pj.task;

import com.pj.api.dto.AppUserDto;
import com.pj.api.dto.MessageDto;
import com.pj.dto.DataDto;
import com.pj.enummj.FinishStatus;
import com.pj.ht_borders_list.HtBordersList;
import com.pj.ht_borders_list.HtBordersListMapper;
import com.pj.ht_byte.HtByte;
import com.pj.ht_byte.HtByteMapper;
import com.pj.project4sp.SP;
import com.pj.retry.SmsRetryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.pj.api.consts.FeignFactory.adminInterface;
import static com.pj.api.consts.FeignFactory.levelOneServerInterface;

/**
 * @Author Mechrevo
 * @Date 2023 08 31 15 59
 **/
@Service
public class MethodHtByteTask {

    @Autowired
    private SmsRetryService smsRetryService;
    @Autowired
    private HtBordersListMapper htBordersListMapper;
    @Autowired
    private HtByteMapper htByteMapper;

    /**
     *  接收订单推送消息，边民进行交易确认，支付申请，刷脸认证，系统发起进行交易
     * @param bordersLists
     */
    public void sendMessageToPeople(List<HtBordersList> bordersLists){
        if(bordersLists.size() == 0)return;
        //遍历
        bordersLists.forEach(item -> {
            try {
                //获取边民appUser
                AppUserDto appUser = levelOneServerInterface.getAppUserByBorderPutrecNo(item.getBorderPutrecNo());
                //todo: 1. 发送系统message
                MessageDto dto = new MessageDto();
                dto.setUrl("/pages/market/one/merchant/order/list");
                dto.setUserId(appUser.getId());
                dto.setContents("你有一笔新订单,请前往app内进行查看。");
                //执行message发送
                adminInterface.messageSave(dto);
                //todo: 2. 发送短信???
//                smsRetryService.sendSmsRegisteMsg(appUser.getPhone(),"你有一笔新订单,请前往app内进行查看。");

                //todo: 3. 调用刷脸组件

                //todo: 4. 在第3步刷脸通过后获取其返回值,判断其是否刷脸通过调用支付申请

                //todo: 5. 获取支付结果,修改完成状态

                item.setFinishStatus(FinishStatus.FINISH_STATUS_ONE.getCode());
                item.setCreateTime(new Date());
                htBordersListMapper.updateById(item);
            } catch (Exception e) {
                item.setFinishStatus(FinishStatus.FINISH_STATUS_TWO.getCode());
                htBordersListMapper.updateById(item);
            }
        });
    }

    /**
     * 修改ht_byte数据的状态
     */
    public void updateHtByteInfo(boolean flag, HtByte htByte,DataDto dataDto){
        //处理成功
        if(flag){
            htByte.setFinishStatus(FinishStatus.FINISH_STATUS_ONE.getCode());
            htByte.setMessageId(dataDto.getMsgId());
            htByte.setDataType(dataDto.getDataType());
            htByte.setCreateTime(new Date());
            htByteMapper.updateById(htByte);
            return;
        }
        //将完成状态设置成 2 , 表示处理失败
        htByte.setFinishStatus(FinishStatus.FINISH_STATUS_TWO.getCode());
        htByte.setMessageId(dataDto.getMsgId());
        htByte.setDataType(dataDto.getDataType());
        htByte.setCreateTime(new Date());
        //保存
        htByteMapper.updateById(htByte);
    }

}
