package com.jeequan.jeepay.mgr.ctrl.aqf;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.MchInfo;
import com.jeequan.jeepay.db.entity.TaskList;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.TaskListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 任务
 */
@RestController
@RequestMapping("/api/taskList")
public class TaskListController extends CommonCtrl {

    @Autowired
    private TaskListService taskListService;

    /**
     * 新增任务
     * @return
     */
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    public ApiRes addTaskList(){
        TaskList taskList = getObject(TaskList.class);
        getValStringRequired("mchNo");
        if (StringUtils.isEmpty(taskList.getAgentNo())) {
            //获取服务商信息
            MchInfo mchInfo = mchInfoService.getById(taskList.getMchNo());
            taskList.setAgentNo(mchInfo.getAgentNo());
        }
        // 当前登录用户信息
        SysUser sysUser = getCurrentUser().getSysUser();
        taskList.setId(DateUtil.format(new Date(), "yyMMdd") + RandomUtil.randomNumbers(6));
        taskList.setCreatedBy(String.valueOf(sysUser.getSysUserId()));
        boolean save = taskListService.save(taskList);
        if (save){
            return ApiRes.ok();
        }else {
            return ApiRes.customFail("新增失败！");
        }
    }
    /**
     * 查询
     * @return
     */
    @RequestMapping(value = "/getPageList" ,method = RequestMethod.GET)
    public ApiRes getlistTaskList(){
        TaskList taskList = getObject(TaskList.class);
        // 时间搜索
        Date[] searchDateRange = taskList.buildQueryDateRange();
        taskList.setFirstDate(searchDateRange[0]);
        taskList.setLastDate(searchDateRange[1]);
        IPage<TaskList> pageList = taskListService.getPageList(getIPage(), taskList);
        //Page<TaskList> page = taskListService.page(getIPage(), TaskList.gw(taskList).orderByDesc(TaskList::getCreatedAt));
        return ApiRes.page(pageList);
    }
    /**
     * 查询详情
     * @return
     */
    @RequestMapping(value = "/{id}" ,method = RequestMethod.GET)
    public ApiRes getByIdTaskList(@PathVariable("id") Integer id){
        TaskList byId = taskListService.getById(id);
        return ApiRes.ok(byId);
    }

    /**
     * 修改
     * @return
     */
    @RequestMapping(value = "/{id}" ,method = RequestMethod.PUT)
    public ApiRes updateTaskList(@PathVariable("id") String id) {
        TaskList reqParam = getObject(TaskList.class);
        reqParam.setId(id);

        boolean resut = taskListService.updateById(reqParam);
        if (resut){
            return ApiRes.ok();
        }else {
            return ApiRes.customFail("修改失败！");
        }
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/{id}" ,method = RequestMethod.DELETE)
    public ApiRes deleteTaskList(@PathVariable("id") Integer id){
        boolean resut = taskListService.removeById(id);
        if (resut){
            return ApiRes.ok();
        }else {
            return ApiRes.customFail("删除失败！");
        }
    }
}
