package cn.estsh.i3plus.core.apiservice.controller.busi;

import cn.estsh.i3plus.core.api.iservice.busi.ISysTaskService;
import cn.estsh.i3plus.icloud.wms.sdk.IWmsCommonCloud;
import cn.estsh.i3plus.platform.common.tool.StringTool;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.bean.BaseResultBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.SysTask;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.base.service.CommonServiceImpl;
import cn.estsh.impp.framework.base.service.ICommonService;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
import cn.estsh.impp.framework.boot.quartz.IQuartzJobService;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description : 任务管理服务
 * @Reference :
 * @Author : yunhao
 * @CreateDate : 2018-12-27 15:13
 * @Modify:
 **/
@RestController
@Api(description = "任务管理服务")
@RequestMapping(PlatformConstWords.BASE_URL + "/task")
public class SysTaskController extends CoreBaseController {

    public static final Logger LOGGER = LoggerFactory.getLogger(SysTaskController.class);

    @Autowired
    private ISysTaskService sysTaskService;

    @Autowired
    private CommonServiceImpl coreCommonService;

    @Autowired
    private IWmsCommonCloud wmsCommonCloud;

    private ICommonService getCommonService(int softType){
        ICommonService commonService = coreCommonService;

        if(softType == CommonEnumUtil.SOFT_TYPE.CORE.getValue()){
            //平台定时任务
            commonService = coreCommonService;
        }else if(softType == CommonEnumUtil.SOFT_TYPE.WMS.getValue()){
            commonService = wmsCommonCloud;
        }
        return commonService;
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加任务", notes = "添加任务")
    public ResultBean insertSysTask(SysTask sysTask) {
        try {
            // 数据校验
            ValidatorBean.beginValid(sysTask)
                    .notNull("name", sysTask.getName())
                    .notNull("taskSoftType", sysTask.getTaskSoftType())
                    .notNull("taskPackage", sysTask.getTaskPackage())
                    .notNull("taskClass", sysTask.getTaskClass());

            // 类校验
            BaseResultBean resultBean = getCommonService(sysTask.getTaskSoftType()).isExistClass(sysTask.getTaskPackage(), sysTask.getTaskClass());
            if(!resultBean.isSuccess()){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("任务类不存在")
                        .setErrorSolution("请重新输入")
                        .build();
            }

            sysTaskService.insertSysTask(sysTask);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(sysTask);
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/update")
    @ApiOperation(value = "修改任务", notes = "修改任务")
    public ResultBean updateSysTask(SysTask sysTask) {
        try {
            // 数据校验
            ValidatorBean.beginValid(sysTask)
                    .notNull("name", sysTask.getName())
                    .notNull("taskSoftType", sysTask.getTaskSoftType())
                    .notNull("taskPackage", sysTask.getTaskPackage())
                    .notNull("taskClass", sysTask.getTaskClass());

            sysTaskService.updateSysTask(sysTask);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation(value = "删除任务", notes = "删除任务")
    public ResultBean deleteSysTaskById(@PathVariable("id") String idStr) {
        try {
            // 数据校验
            ValidatorBean.checkNotNull(idStr, "id不能为空");
            if (sysTaskService.getSysTaskById(Long.parseLong(idStr)) == null) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("任务不存在")
                        .setErrorSolution("请重新选择")
                        .build();
            }

            sysTaskService.deleteSysTaskById(Long.parseLong(idStr));
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/list")
    @ApiOperation(value = "查询所有任务", notes = "查询所有任务")
    public ResultBean listSysTask() {
        try {
            List sysTaskList = sysTaskService.listSysTask();
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultList(sysTaskList);
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/query")
    @ApiOperation(value = "任务复杂查询", notes = "任务复杂查询,分页，排序")
    public ResultBean querySysTaskByPager(SysTask sysTask, Pager pager) {
        try {
            ListPager<SysTask> sysTaskList = sysTaskService.querySysTaskByPager(sysTask, pager);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setListPager(sysTaskList);
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @DeleteMapping(value = "/batch-delete")
    @ApiOperation(value = "根据id批量删除任务", notes = "根据id批量删除任务")
    public ResultBean deleteSysTaskByIds(String[] idsStr) {
        try {
            if (idsStr == null || idsStr.length == 0) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要删除的资源。")
                        .build();
            }
            sysTaskService.deleteSysTaskByIds(StringTool.getArrayLong(idsStr));
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
