package com.chinaitop.depot.intelligent.IntelligentDetection.controller;

import com.chinaitop.depot.intelligent.IntelligentDetection.model.TTimingTask;
import com.chinaitop.depot.intelligent.IntelligentDetection.model.TTimingTaskHistory;
import com.chinaitop.depot.intelligent.IntelligentDetection.service.TTimingTaskService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unissoft.model.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author qingsong.han
 * @description: 粮情检查分析
 * @create 2018-12-16 16:55
 */
@RestController
@RequestMapping(value = "/intelligents/task")
@Api(value = "IntelligentBatchDetectionController", description = "智能批量检测")
@SuppressWarnings("all")
public class IntelligentBatchDetectionController {

    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;

    @Resource
    TTimingTaskService tTimingTaskService;

    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @param tName
     * @param tState
     * @return
     */
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ApiOperation(value = "智能检测信息列表", notes = "智能检测信息列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", defaultValue = "1", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", defaultValue = "10", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "tName", value = "机构id", paramType = "query"),
            @ApiImplicitParam(name = "tState", value = "是否开启", paramType = "query"),
            @ApiImplicitParam(name = "tCategory", value = "检测类型", paramType = "query")
    })
    public ResponseEntity<PageInfo<TTimingTask>> getList(Integer pageNum, Integer pageSize, String tName, String tState, String tCategory) {
        List<TTimingTask> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = tTimingTaskService.getList(tName,tState,tCategory);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        PageInfo<TTimingTask> pageInfo = new PageInfo<>(list);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 保存,修改
     * @param taskSchemaJson
     * @return
     */
    @RequestMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "数据更新与添加", notes = "数据更新与添加")
    @ApiImplicitParam(name = "taskSchemaJson", value = "表数据json串", paramType = "form")
    public ResponseEntity save(String taskSchemaJson) {
        try {
            tTimingTaskService.save(taskSchemaJson);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("保存失败");
        }
        return ResponseEntity.ok();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @ApiImplicitParam(name = "id", value = "策略设置id", paramType = "query")
    public ResponseEntity deleteById(String id) {
        if (StringUtils.isNotBlank(id)) {
            int count = tTimingTaskService.deleteById(id);
            if (count > 0) {
                return ResponseEntity.ok();
            } else {
                return ResponseEntity.failed("删除失败!");
            }
        }
        return ResponseEntity.failed("ID数据有误!");
    }

    /**
     * 根据策略id查询
     * @param id
     * @return
     */
    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    @ApiOperation(value = "根据id查询 信息", notes = "根据id查询 信息")
    @ApiImplicitParam(name = "id", value = "策略设置id", paramType = "query")
    public ResponseEntity<TTimingTask> getById(String tId) {
        TTimingTask timingTask = null;
        if (StringUtils.isNotBlank(tId)) {
            timingTask = tTimingTaskService.getById(tId);
        } else {
            return ResponseEntity.failed("ID数据有误!");
        }
        return ResponseEntity.ok(timingTask);
    }

    @RequestMapping(value = "/getDetectionHistory", method = RequestMethod.GET)
    @ApiOperation(value = "智能检测信息历史数据", notes = "智能检测信息历史数据，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", defaultValue = "1", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", defaultValue = "10", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "vcfcode", value = "仓房code", paramType = "query"),
            @ApiImplicitParam(name = "time", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "tid", value = "检测id", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "所属类型", paramType = "query")
    })
    public ResponseEntity<PageInfo<TTimingTaskHistory>> getDetectionHistory(Integer pageNum, Integer pageSize, String vcfcode, String time, String tid,String type) {
        List<TTimingTaskHistory> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = tTimingTaskService.getDetectionHistory(vcfcode,time,tid,type);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        PageInfo<TTimingTaskHistory> pageInfo = new PageInfo<>(list);
        return ResponseEntity.ok(pageInfo);
    }
}
