package com.pusong.business.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.pusong.business.domain.PsTaskAppoint;
import com.pusong.business.domain.bo.AppointBo;
import com.pusong.business.domain.bo.MediaCompanySaveBo;
import com.pusong.business.domain.bo.PsTaskQueryBo;
import com.pusong.business.domain.vo.PsTaskAppointVo;
import com.pusong.business.domain.vo.TaskPlanVo;
import com.pusong.business.service.IPsContractInfoService;
import com.pusong.common.core.domain.R;
import com.pusong.common.core.validate.QueryGroup;
import com.pusong.common.idempotent.annotation.RepeatSubmit;
import com.pusong.common.log.annotation.Log;
import com.pusong.common.log.enums.BusinessType;
import com.pusong.common.web.core.BaseController;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.pusong.common.mybatis.core.page.PageQuery;
import com.pusong.business.domain.vo.PsTaskMainVo;
import com.pusong.business.service.IPsTaskService;
import com.pusong.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 任务controller
 *
 * @author wls
 * @date 2024-08-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/task")
@SaIgnore
public class PsTaskController extends BaseController {

    private final IPsTaskService psTaskMainService;
    private final IPsContractInfoService contractInfoService;
    /**
     * 查询主任务列表
     */
    @Log(title = "查询主任务列表")
    @SaCheckPermission("business:task:list")
    @GetMapping("/list")
    public TableDataInfo<PsTaskMainVo> list(PsTaskQueryBo bo, PageQuery pageQuery) {
        return psTaskMainService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询内外特任务列表
     */
    @Log(title = "查询内外特任务列表")
    @SaCheckPermission("business:task:list")
    @GetMapping("/childList")
    public TableDataInfo<PsTaskMainVo> childList(@Validated(QueryGroup.class) PsTaskQueryBo bo, PageQuery pageQuery) {
        return psTaskMainService.queryChildPageList(bo, pageQuery);
    }
    /**
     * 根据主任务id查询进度
     */
    @Log(title = "根据主任务id查询进度")
    @SaCheckPermission("business:task:list")
    @GetMapping("/plan")
    public R<TaskPlanVo> plan(@NotNull Long id) {
        return R.ok(psTaskMainService.plan(id));
    }

    /**
     * 根据任务id查询任务的公司信息和资料信息
     */
    @Log(title = "根据任务id查询任务的公司信息和资料信息")
    @SaCheckPermission("business:task:list")
    @GetMapping("/info")
    public R<PsTaskMainVo> info(@NotBlank String id) {
        return R.ok(psTaskMainService.queryInfo(id));
    }

    /**
     * 上传资料和公司信息
     */
    @Log(title = "上传资料和公司信息")
    @SaCheckPermission("business:task:list")
    @PostMapping("/saveInfo")
    @RepeatSubmit()
    public R<Void> saveInfo(@Validated @RequestBody MediaCompanySaveBo save) {
        psTaskMainService.saveInfo(save);
        return R.ok();
    }


    /**
     * 任务委派
     */
    @Log(title = "任务委派")
    @SaCheckPermission("business:task:list")
    @PostMapping("/appoint")
    @RepeatSubmit()
    public R<Void> appoint(@Validated @RequestBody AppointBo appointBo) {
        psTaskMainService.appoint(appointBo);
        return R.ok();
    }

    /**
     * 主任务完成
     * @param id 主任务id
     * @return
     */
    @RepeatSubmit()
    @Log(title = "主任务完成")
    @SaCheckPermission("business:task:list")
    @GetMapping("/mainFinish")
    public R<Void> mainFinish(@NotNull Long id) {
        contractInfoService.finish(psTaskMainService.finishMain(id));
        return R.ok();
    }

    /**
     * 子任务完成
     * @param id 子任务id
     * @param desc 特勤备注
     * @return
     */
    @Log(title = "子任务完成")
    @SaCheckPermission("business:task:list")
    @GetMapping("/childFinish")
    @RepeatSubmit()
    public R<Void> childFinish(@NotNull Long id,String desc) {
        psTaskMainService.finishChild(id,desc);
        return R.ok();
    }

    /**
     * 获取任务执行者列表
     * @param id 任务id
     * @return
     */
    @Log(title = "获取任务执行者列表")
    @SaCheckPermission("business:task:list")
    @GetMapping("/executors")
    public R<Map<String, PsTaskAppointVo>> executors(@NotNull Long id) {
        return R.ok(psTaskMainService.getExecutors(id));
    }

    /**
     * 任务改派
     * @param id 任务委派表id
     * @param executorId 任务新的执行者id
     * @return
     */
    @Log(title = "任务改派")
    @SaCheckPermission("business:task:list")
    @GetMapping("/reset")
    @RepeatSubmit()
    public R<Void> taskReset(@NotNull Long id,@NotNull Long executorId) {
        psTaskMainService.taskReset(id, executorId);
        return R.ok();
    }

    /**
     * 免费工单任务作废
     * @param id 任务主表id
     * @param desc 作废原因
     * @return
     */
    @Log(title = "免费工单任务作废")
    @SaCheckPermission("business:task:list")
    @GetMapping("/cancel")
    @RepeatSubmit()
    public R<Void> taskReset(@NotNull("任务id不能为空") Long id,@NotNull("作废原因不能为空") String desc) {
        psTaskMainService.cancel(id, desc);
        return R.ok();
    }

    /**
     * 上传发票
     * @param id 任务id
     * @param file 发票文件
     * @return
     */
    @Log(title = "上传发票")
    @SaCheckPermission("business:task:list")
    @PostMapping(value = "/invoice", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @RepeatSubmit()
    public R<Void> taskReset(@RequestPart("file") MultipartFile file, @NotNull Long id) {
        psTaskMainService.invoice(file,id);
        return R.ok();
    }

    /**
     * 任务暂停
     * @param id 任务id
     * @return
     */
    @Log(title = "任务暂停")
    @SaCheckPermission("business:task:list")
    @GetMapping("/pause")
    @RepeatSubmit()
    public R<Void> pause( @NotNull Long id) {
        psTaskMainService.pause(id);
        return R.ok();
    }

    /**
     * 任务恢复
     * @param id 任务id
     * @return
     */
    @Log(title = "任务恢复")
    @SaCheckPermission("business:task:list")
    @GetMapping("/recover")
    @RepeatSubmit()
    public R<Void> recover( @NotNull Long id) {
        psTaskMainService.recover(id);
        return R.ok();
    }

//    /**
//     * 导出主任务列表
//     */
//    @SaCheckPermission("business:taskMain:export")
//    @Log(title = "主任务", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(PsTaskMainBo bo, HttpServletResponse response) {
//        List<PsTaskMainVo> list = psTaskMainService.queryList(bo);
//        ExcelUtil.exportExcel(list, "主任务", PsTaskMainVo.class, response);
//    }
//
//    /**
//     * 获取主任务详细信息
//     *
//     * @param id 主键
//     */
//    @SaCheckPermission("business:taskMain:query")
//    @GetMapping("/{id}")
//    public R<PsTaskMainVo> getInfo(@NotNull(message = "主键不能为空")
//                                     @PathVariable Long id) {
//        return R.ok(psTaskMainService.queryById(id));
//    }
//
//    /**
//     * 新增主任务
//     */
//    @SaCheckPermission("business:taskMain:add")
//    @Log(title = "主任务", businessType = BusinessType.INSERT)
//    @RepeatSubmit()
//    @PostMapping()
//    public R<Void> add(@Validated(AddGroup.class) @RequestBody PsTaskMainBo bo) {
//        return toAjax(psTaskMainService.insertByBo(bo));
//    }
//
//    /**
//     * 修改主任务
//     */
//    @SaCheckPermission("business:taskMain:edit")
//    @Log(title = "主任务", businessType = BusinessType.UPDATE)
//    @RepeatSubmit()
//    @PutMapping()
//    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PsTaskMainBo bo) {
//        return toAjax(psTaskMainService.updateByBo(bo));
//    }
//
//    /**
//     * 删除主任务
//     *
//     * @param ids 主键串
//     */
//    @SaCheckPermission("business:taskMain:remove")
//    @Log(title = "主任务", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public R<Void> remove(@NotEmpty(message = "主键不能为空")
//                          @PathVariable Long[] ids) {
//        return toAjax(psTaskMainService.deleteWithValidByIds(List.of(ids), true));
//    }
}
