package org.dromara.payment.task.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.payment.merchant.domain.bo.BusMerchantProductBo;
import org.dromara.payment.merchant.domain.vo.BusMerchantProductVo;
import org.dromara.payment.task.domain.MerTaskDetail;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.payment.task.domain.vo.MerTaskVo;
import org.dromara.payment.task.domain.bo.MerTaskBo;
import org.dromara.payment.task.service.IMerTaskService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 任务
 * 前端访问路由地址为:/payment/task
 *
 * @author LionLi
 * @date 2024-04-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/task")
public class MerTaskController extends BaseController {

    private final IMerTaskService merTaskService;

    /**
     * 查询任务列表  商户角色 查看自己发起得；  运营商 查询自己得； 服务商接口
     */
//    @SaCheckPermission("payment:task:list")
    @GetMapping("/list")
    public TableDataInfo<MerTaskVo> list(MerTaskBo bo, PageQuery pageQuery) {
        return merTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * @description 运营商审核列表， 服务商审核列表
     * @author wuchuang
     * @param
     * @return
     * @time 2024/5/17 9:40
     */
//    @SaCheckPermission("payment:task:checkList")
    @GetMapping("/checkList")
    public TableDataInfo<MerTaskVo> checkList(MerTaskBo bo, PageQuery pageQuery) {
        return merTaskService.queryCheckList(bo, pageQuery);
    }


    /**
     * 获取任务详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("payment:task:query")
    @GetMapping("/{id}")
    public R<MerTaskDetail> queryById(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id, PageQuery pageQuery) {
        return R.ok(merTaskService.queryById(id,pageQuery));
    }

    /**
     * 新增任务
     */
//    @SaCheckPermission("payment:task:add")
    @Log(title = "任务新建", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MerTaskBo bo) {
        return toAjax(merTaskService.insertByBo(bo));
    }

    /**
     * 修改任务 发布上线接口
     */
//    @SaCheckPermission("payment:task:edit")
    @Log(title = "商户编辑、发布", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MerTaskBo bo) {
        return toAjax(merTaskService.updateByBo(bo));
    }

    /**
     * 修改基本信息 服务商 运营商 角色使用
     */
//    @SaCheckPermission("payment:task:edit")
    @Log(title = "任务修改信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/editInfo")
    public R<Void> editInfo(@Validated(EditGroup.class) @RequestBody MerTaskBo bo) {
        return toAjax(merTaskService.updateInfoByBo(bo));
    }


    /**
     * 任务审核 提交
     */
//    @SaCheckPermission("payment:task:edit")
    @Log(title = "任务审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/check")
    public R<Void> check(@Validated(EditGroup.class) @RequestBody MerTaskBo bo) {
        return toAjax(merTaskService.checkInfoByBo(bo));
    }


    /**
     * 商户驳回提交
     */
//    @SaCheckPermission("payment:task:edit")
    @Log(title = "被驳回后提交", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/rejectCommit")
    public R<Void> rejectCommit(@Validated(EditGroup.class) @RequestBody MerTaskBo bo) {
        return toAjax(merTaskService.rejectCommitByBo(bo));
    }


    @GetMapping("/queryList")
    public R<List<MerTaskVo>> queryList(MerTaskBo bo) {
        return R.ok(merTaskService.queryList(bo));
    }

    /**
     * 上架下架
     */
//    @SaCheckPermission("payment:task:edit")
    @Log(title = "任务上架下架", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/ups")
    public R<Void> ups(@Validated(EditGroup.class) @RequestBody MerTaskBo bo) {
        return toAjax(merTaskService.updateStateByBo(bo));
    }


    /**
     * 导出任务列表
     */
    @SaCheckPermission("payment:task:export")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MerTaskBo bo, HttpServletResponse response) {
        List<MerTaskVo> list = merTaskService.queryList(bo);
        ExcelUtil.exportExcel(list, "任务", MerTaskVo.class, response);
    }


    /**
     * 删除任务
     *
     * @param ids 主键串
     */
    @SaCheckPermission("payment:task:remove")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(merTaskService.deleteWithValidByIds(List.of(ids), true));
    }
}
