package com.sptg.device.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.device.api.entity.InstructPublishLog;
import com.sptg.device.api.feigns.InstructPublishLogFeign;
import com.sptg.device.biz.service.InstructPublishLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

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

/**
 *
 *
 * description:
 */
@RestController
@RequiredArgsConstructor
public class InstructPublishLogController implements InstructPublishLogFeign {

    private final InstructPublishLogService instructPublishLogService;

    @Override
    @PostMapping(value = "/instruct/callback")
    public ResultBean<Boolean> instructCallback(@RequestBody InstructPublishLog publishLog) {
        InstructPublishLog instructPublishLog = this.instructPublishLogService.selectById(publishLog.getId());
        Assert.notNull(instructPublishLog,"Id不存在");
        instructPublishLog.setStatus(publishLog.getStatus());
        instructPublishLog.setErrorMsg(publishLog.getErrorMsg());
        instructPublishLog.setLastModifyTime(new Date());
        this.instructPublishLogService.updateById(instructPublishLog);
        return ResultBean.ok(true);
    }

    @Override
    @PostMapping(value = "/instruct/biz/callback")
    public ResultBean<Boolean> instructBizCallback(@RequestBody InstructPublishLog publishLog) {
        InstructPublishLog instructPublishLog = this.instructPublishLogService.selectById(publishLog.getId());
        Assert.notNull(instructPublishLog,"Id不存在");
        instructPublishLog.setBizStatus(publishLog.getHandleStatus());
        if(publishLog.getErrorMsg() != null){
            instructPublishLog.setErrorMsg(publishLog.getErrorMsg());
        }
        this.instructPublishLogService.updateById(instructPublishLog);
        return ResultBean.ok(true);
    }

    @Override
    @PostMapping(value = "/instruct/handle/callback")
    public ResultBean<Boolean> instructHandleCallback(@RequestBody InstructPublishLog publishLog) {
        InstructPublishLog instructPublishLog = this.instructPublishLogService.selectById(publishLog.getId());
        Assert.notNull(instructPublishLog,"Id不存在");
        instructPublishLog.setHandleStatus(publishLog.getHandleStatus());
        this.instructPublishLogService.updateById(instructPublishLog);
        return ResultBean.ok(true);
    }

    @Override
    @PostMapping(value = "dh/instruct/callback")
    public ResultBean<Boolean> dhInstructCallback(@RequestBody InstructPublishLog publishLog) {
        InstructPublishLog instructPublishLog = this.instructPublishLogService.selectById(publishLog.getId());
        if(instructPublishLog != null){
            instructPublishLog.setStatus(publishLog.getStatus());
            instructPublishLog.setErrorMsg(publishLog.getErrorMsg());
            instructPublishLog.setLastModifyTime(new Date());
            this.instructPublishLogService.updateById(instructPublishLog);
        }
        return ResultBean.ok(true);
    }

    @Override
    @PostMapping(value = "/instruct/pagingQuery")
    public ResultBean<Page> pagingQuery(@RequestBody PageBean<InstructPublishLog, InstructPublishLog> pageBean) {
        return ResultBean.ok(this.instructPublishLogService.pagingQuery(pageBean));
    }

    @Override
    @PostMapping(value = "/instruct/pageFace")
    public ResultBean<Page> pageFace(@RequestBody PageBean<InstructPublishLog, InstructPublishLog> pageBean) {
        return ResultBean.ok(this.instructPublishLogService.pageFace(pageBean));
    }

    @Override
    @PostMapping(value = "/instruct/pageCard")
    public ResultBean<Page> pageCard( @RequestBody PageBean<InstructPublishLog, InstructPublishLog> pageBean) {
        return ResultBean.ok(this.instructPublishLogService.pageCard(pageBean));
    }

    @Override
    @PostMapping(value = "/instruct/pageAppPackage")
    public ResultBean<Page> pageAppPackage( @RequestBody PageBean<InstructPublishLog, InstructPublishLog> pageBean) {
        return ResultBean.ok(this.instructPublishLogService.pageAppPackage(pageBean));
    }

    @Override
    @GetMapping(value = "/instruct/get/{id}")
    public InstructPublishLog getById(@PathVariable(value = "id") Long id) {
        return this.instructPublishLogService.selectById(id);
    }

    @Override
    @PostMapping(value = "/instruct/getListPutBin")
    public List<InstructPublishLog> getListPutBin(@RequestBody InstructPublishLog instructPublishLog){
        return this.instructPublishLogService.getListPutBin(instructPublishLog);
    }

    @Override
    @PostMapping(value = "/instruct/deletePutBinList")
    public ResultBean<Boolean> deletePutBinList(@RequestBody List<InstructPublishLog> logList){
        return this.instructPublishLogService.deletePutBinList(logList);
    }

    @Override
    @PostMapping(value = "/instruct/findInstructPublishLogByCondition")
    public List<InstructPublishLog> findInstructPublishLogByCondition(@RequestBody InstructPublishLog instructPublishLog){
        return this.instructPublishLogService.findInstructPublishLogByCondition(instructPublishLog);
    }

    @Override
    @PostMapping(value = "/instruct/create")
    public ResultBean<Boolean> create(@RequestBody InstructPublishLog instructPublishLog) {
        boolean insert = this.instructPublishLogService.insert(instructPublishLog);
        if(insert){
            return new ResultBean<>(ResultEnum.SUCCESS);
        }else{
            return new ResultBean<>(ResultEnum.ERROR);
        }
    }
//    @Override
//    @PostMapping(value = "/instruct/resend")
//    public ResultBean<Boolean> resendInstrcut(@RequestBody InstructPublishLog instructPublishLog) {
//        String serialNumber = instructPublishLog.getSerialNumber();
//        Asserts.notBlank(serialNumber, "设备序列号不能为空");
//        Device device = this.deviceServiceInterface.selectBySerialNumber(serialNumber);
//        Asserts.notNull(device, "设备不存在，不能下发指令");
//        if(DeviceConstants.DEVICE_FACTORY_DAHAO.equals(device.getDeviceFactory())){
//            return this.instructPublishLogService.resendInstructDH(instructPublishLog);
//        }
//        return this.instructPublishLogService.resendInstruct(instructPublishLog);
//    }
//
//    @Override
//    @PostMapping(value = "/instruct/dh/resend")
//    public ResultBean<Boolean> resendInstrcutDH(@RequestBody InstructPublishLog instructPublishLog) {
//        return this.instructPublishLogService.resendInstructDH(instructPublishLog);
//    }
}
