package com.sptg.estate.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.entity.NoticePush;
import com.sptg.estate.api.entity.NoticePushDeviceDetails;
import com.sptg.estate.api.feigns.NoticePushDetailsFeign;
import com.sptg.estate.biz.service.NoticePushDeviceDetailsServiceInterface;
import com.sptg.estate.biz.service.NoticePushServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * title: NoticePushDetailsController
 * package: com.sptg.estate.biz.controller
 * description:
 *
 *                  
 *               
 */
@RestController
@RequiredArgsConstructor
public class NoticePushDetailsController implements NoticePushDetailsFeign {

    private final NoticePushServiceInterface noticePushServiceInterface;
    private final NoticePushDeviceDetailsServiceInterface noticePushDeviceDetailsServiceInterface;

    /* -------------------------------------------------------------------------------------------------------------- */

    @Override
    @PostMapping(value = "/notice/push/task/details/updatePushStatus")
    @Transactional(rollbackFor = Exception.class)
    public ResultBean<Boolean> updatePushStatus(@RequestBody NoticePushDeviceDetails dto) {
        Wrapper<NoticePushDeviceDetails> wrapper = new EntityWrapper<>();
        wrapper.eq("l_task_id",dto.getTaskId());
        wrapper.eq("s_serial_number",dto.getSerialNumber());
        boolean result = this.noticePushDeviceDetailsServiceInterface.update(dto, wrapper);
        Assert.isTrue(result,"任务Id或设备序列号不存在");
        //  判断是否推送成功
        if (dto.getPushStatus() == OperationConstant.ONE){
            NoticePush noticePush = this.noticePushServiceInterface.selectById(dto.getTaskId());
            if (null == noticePush.getPushSuccess()){
                noticePush.setPushSuccess(0);
            }
            noticePush.setPushSuccess(noticePush.getPushSuccess() + 1);
            result = this.noticePushServiceInterface.updateById(noticePush);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }



}
