package com.ruoyi.hywz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hywz.entity.AdsMatHyxtTaskHistoryD;
import com.ruoyi.hywz.entity.AdsMatHyxtTaskRunD;
import com.ruoyi.hywz.entity.po.TaskQueryPO;
import com.ruoyi.hywz.service.IAdsMatHyxtTaskHistoryDService;
import com.ruoyi.hywz.service.IAdsMatHyxtTaskRunDService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author: jxh
 * @description:
 * @create: 2024/4/8
 */
@Api(value = "adsMatHyxtTaskD", tags = "任务相关接口")
@RestController
@AllArgsConstructor
@RequestMapping("adsMatHyxtTaskD")
public class AdsMatHyxtTaskDController {

    private final IAdsMatHyxtTaskRunDService adsMatHyxtTaskRunDService;
    private final IAdsMatHyxtTaskHistoryDService adsMatHyxtTaskHistoryDService;

    @ApiOperation(value = "任务数量", notes = "任务数量")
    @GetMapping("/taskNum")
    public AjaxResult taskNum() {
        return AjaxResult.success(adsMatHyxtTaskRunDService.getTaskNum());
    }

    @ApiOperation(value = "【待办任务】分页查询", notes = "【待办任务】分页查询")
    @PostMapping("/todoPage")
    public R<Page<AdsMatHyxtTaskRunD>> todoPage(@RequestParam(defaultValue = "10") Long pageSize,
                                          @RequestParam(defaultValue = "1") Long pageNo,
                                          @RequestBody(required = false) TaskQueryPO param) {
        Page<AdsMatHyxtTaskRunD> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        LoginUser loginUser = SecurityUtils.getLoginUser();

        QueryWrapper<AdsMatHyxtTaskRunD> queryWrapper = Wrappers.query();
        queryWrapper.eq("owner_user", loginUser.getUsername());
        if (param != null) {
            queryWrapper.like(StringUtils.isNotEmpty(param.getTaskName()), "task_name", param.getTaskName());
            queryWrapper.like(StringUtils.isNotEmpty(param.getApplyUserName()), "apply_user_name", param.getApplyUserName());
            if (StringUtils.isNotEmpty(param.getStartDate()) && StringUtils.isNotEmpty(param.getEndDate())) {
                queryWrapper.ge("DATE_FORMAT(create_time,'%Y-%m-%d')", param.getStartDate());
                queryWrapper.le("DATE_FORMAT(create_time,'%Y-%m-%d')", param.getEndDate());
            }
        }
        queryWrapper.eq("is_deleted", 0);
        queryWrapper.orderByDesc("create_time");
        return R.ok(adsMatHyxtTaskRunDService.page(page, queryWrapper));
    }

    @ApiOperation(value = "【已办任务】分页查询", notes = "【已办任务】分页查询")
    @PostMapping("/finishPage")
    public R<Page<AdsMatHyxtTaskHistoryD>> finishPage(@RequestParam(defaultValue = "10") Long pageSize,
                                 @RequestParam(defaultValue = "1") Long pageNo,
                                 @RequestBody(required = false) TaskQueryPO param) {
        Page<AdsMatHyxtTaskHistoryD> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        LoginUser loginUser = SecurityUtils.getLoginUser();

        QueryWrapper<AdsMatHyxtTaskHistoryD> queryWrapper = Wrappers.query();
        queryWrapper.eq("owner_user", loginUser.getUsername());
        if (param != null) {
            queryWrapper.like(StringUtils.isNotEmpty(param.getTaskName()), "task_name", param.getTaskName());
            queryWrapper.like(StringUtils.isNotEmpty(param.getApplyUserName()), "apply_user_name", param.getApplyUserName());
            if (StringUtils.isNotEmpty(param.getStartDate()) && StringUtils.isNotEmpty(param.getEndDate())) {
                queryWrapper.ge("DATE_FORMAT(create_time,'%Y-%m-%d')", param.getStartDate());
                queryWrapper.le("DATE_FORMAT(create_time,'%Y-%m-%d')", param.getEndDate());
            }
        }
        queryWrapper.eq("is_deleted", 0);
        queryWrapper.orderByDesc("create_time");
        return R.ok(adsMatHyxtTaskHistoryDService.page(page, queryWrapper));
    }

}
