package com.sptg.device.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.dto.TimeStrategyPushTaskDto;
import com.sptg.device.api.entity.TimeStrategy;
import com.sptg.device.api.entity.TimeStrategyPushTask;
import com.sptg.device.api.entity.TimeStrategyTaskConf;
import com.sptg.device.api.entity.TimeStrategyTaskDeviceDetails;
import com.sptg.device.api.feigns.TimeStrategyPushTaskFeign;
import com.sptg.device.biz.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * title: TimeStrategyPushTaskController
 * package: com.sptg.device.biz.controller
 * description: 时间策略发布任务-控制器
 *
 *                
 *                
 */
@RestController
@RequiredArgsConstructor
public class TimeStrategyPushTaskController implements TimeStrategyPushTaskFeign {
    private final TimeStrategyServiceInterface timeStrategyServiceInterface;
    private final TimeStrategyPushTaskServiceInterface timeStrategyPushTaskServiceInterface;
    private final TimeStrategyTaskDetailsServiceInterface timeStrategyTaskDetailsServiceInterface;
    private final TimeStrategyTaskConfServiceInterface timeStrategyTaskConfServiceInterface;
    private final TimeStrategyTaskDeviceDetailsServiceInterface timeStrategyTaskDeviceDetailsServiceInterface;

    @Override
    @PostMapping(value = "/time/strategy/push/task/pagingQuery")
    public ResultBean<Page<TimeStrategyPushTask>> pagingQuery(@RequestBody TimeStrategyPushTaskDto dto) {
        Page<TimeStrategyPushTask> page=timeStrategyPushTaskServiceInterface.showListTimeStrategyPushTaskList(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,page);
    }

    @Override
    @PostMapping(value = "/time/strategy/push/task/addEntity")
    public ResultBean<Boolean> addEntity(@RequestBody TimeStrategyPushTaskDto dto) {
        TimeStrategy timeStrategy = this.timeStrategyServiceInterface.selectById(dto.getTimeStrategyId());
        Assert.notNull(timeStrategy,"时间策略Id不存在");
        boolean result=timeStrategyPushTaskServiceInterface.addTimeStrategyPushTask(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/time/strategy/push/task/getEntityById")
    public TimeStrategyPushTask getEntityById(@RequestBody TimeStrategyPushTaskDto dto) {
        TimeStrategyPushTask timeStrategyPushTask = this.timeStrategyPushTaskServiceInterface.selectById(dto.getId());
        if (null != timeStrategyPushTask){
            //  权限校验
            if (!timeStrategyPushTask.getOrgPosition().startsWith(dto.getOrgPosition())){
                return null;
            }
            //获取时间段参数
            Wrapper<TimeStrategyTaskConf> wrapper=new EntityWrapper<>();
            wrapper.eq("l_task_id",timeStrategyPushTask.getId());
            List<TimeStrategyTaskConf> timeStrategyTaskConfList = timeStrategyTaskConfServiceInterface.selectList(wrapper);
            timeStrategyPushTask.setTimeStrategyTaskConfList(timeStrategyTaskConfList);
        }
        return timeStrategyPushTask;
    }

    @Override
    @GetMapping(value = "/time/strategy/push/task/getFkIdListByTaskId/{taskId}")
    public List<Long> getFkIdListByTaskId(@PathVariable(value = "taskId") Long taskId){
       return timeStrategyTaskDetailsServiceInterface.getFkIdListByTaskId(taskId);
    }

    @Override
    @PostMapping(value = "/time/strategy/push/task/rollback")
    public ResultBean<Boolean> rollback(@RequestBody TimeStrategyPushTaskDto dto) {
        TimeStrategyPushTask timeStrategyPushTask = this.timeStrategyPushTaskServiceInterface.selectById(dto.getId());
        Assert.notNull(timeStrategyPushTask,"时间策略发布任务Id不存在");
        //  权限校验
        if (!timeStrategyPushTask.getOrgPosition().startsWith(dto.getOrgPosition())){
            return new ResultBean<>(ResultEnum.FORBIDDEN);
        }
        timeStrategyPushTask.setPushStatus(OperationConstant.THREE);
        boolean result=timeStrategyPushTaskServiceInterface.updateById(timeStrategyPushTask);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/time/strategy/push/task/details/updatePushStatus")
    public ResultBean<Boolean> updatePushStatus(@RequestBody TimeStrategyTaskDeviceDetails details) {
        Wrapper<TimeStrategyTaskDeviceDetails> wrapper = new EntityWrapper<>();
        wrapper.eq("l_task_id",details.getTaskId());
        wrapper.eq("s_serial_number",details.getSerialNumber());
        boolean result = this.timeStrategyTaskDeviceDetailsServiceInterface.update(details,wrapper);
        Assert.isTrue(result,"策略任务Id或设备序列号不存在");
        if (details.getPushStatus() == OperationConstant.ONE){
            TimeStrategyPushTask task = this.timeStrategyPushTaskServiceInterface.selectById(details.getTaskId());
            task.setPushSuccess(task.getPushSuccess() + 1);
            result = this.timeStrategyPushTaskServiceInterface.updateById(task);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }
}
