package com.taais.biz.controller;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.excel.utils.ExcelUtil;
import com.taais.common.log.annotation.Log;
import com.taais.common.log.enums.BusinessType;
import com.taais.common.web.annotation.RepeatSubmit;
import com.taais.common.web.core.BaseController;
import jakarta.annotation.Resource;
import com.taais.biz.domain.vo.TargetIdentificationSubtaskVo;
import com.taais.biz.domain.bo.TargetIdentificationSubtaskBo;
import com.taais.biz.service.ITargetIdentificationSubtaskService;

import com.taais.common.core.core.page.PageResult;

/**
 * 目标识别子任务Controller
 *
 * @author Allen
 * 2024-08-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/identification/identificationSubtask")
public class TargetIdentificationSubtaskController extends BaseController {
    @Resource
    private ITargetIdentificationSubtaskService targetIdentificationSubtaskService;

    /**
     * 查询目标识别子任务列表
     */
    @SaCheckPermission("identification:identificationSubtask:list")
    @GetMapping("/list")
    public CommonResult<PageResult<TargetIdentificationSubtaskVo>> list(TargetIdentificationSubtaskBo targetIdentificationSubtaskBo, Integer pageNum, Integer pageSize) {
        List<TargetIdentificationSubtaskVo> list = targetIdentificationSubtaskService.selectList(targetIdentificationSubtaskBo);
        List<TargetIdentificationSubtaskVo> collect = list.stream().sorted(((o1, o2) -> {
                if ("数据扩增".equals(o1.getName())) {
                    return -1;
                }
                if ("数据扩增".equals(o2.getName())) {
                    return -1;
                }
                return 1;
            })).skip((pageNum-1)*pageSize)
            .limit(pageSize).toList();
        PageResult<TargetIdentificationSubtaskVo> result = new PageResult<>();
        result.setList(collect);
        result.setTotal((long) list.size());
        result.setPageNum(Long.valueOf(pageNum));
        result.setPageSize(Long.valueOf(pageSize));
        return CommonResult.success(result);
    }

    /**
     * 导出目标识别子任务列表
     */
    @SaCheckPermission("identification:identificationSubtask:export")
    @Log(title = "目标识别子任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TargetIdentificationSubtaskBo targetIdentificationSubtaskBo) {
        List<TargetIdentificationSubtaskVo> list = targetIdentificationSubtaskService.selectList(targetIdentificationSubtaskBo);
        ExcelUtil.exportExcel(list, "目标识别子任务", TargetIdentificationSubtaskVo.class, response);
    }

    /**
     * 获取目标识别子任务详细信息
     */
    @SaCheckPermission("identification:identificationSubtask:query")
    @GetMapping(value = "/{id}")
    public CommonResult<TargetIdentificationSubtaskVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(targetIdentificationSubtaskService.selectById(id));
    }

    /**
     * 新增目标识别子任务
     */
    @SaCheckPermission("identification:identificationSubtask:add")
    @Log(title = "目标识别子任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody TargetIdentificationSubtaskBo targetIdentificationSubtaskBo) {
        boolean inserted = targetIdentificationSubtaskService.insert(targetIdentificationSubtaskBo);
        if (!inserted) {
            return CommonResult.fail("新增目标识别子任务记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改目标识别子任务
     */
    @SaCheckPermission("identification:identificationSubtask:edit")
    @Log(title = "目标识别子任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody TargetIdentificationSubtaskBo targetIdentificationSubtaskBo) {
        Boolean updated = targetIdentificationSubtaskService.update(targetIdentificationSubtaskBo);
        if (!updated) {
            return CommonResult.fail("修改目标识别子任务记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除目标识别子任务
     */
    @SaCheckPermission("identification:identificationSubtask:remove")
    @Log(title = "目标识别子任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = targetIdentificationSubtaskService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除目标识别子任务记录失败!");
        }
        return CommonResult.success();
    }
}
