package com.bs.ct.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import com.bs.ct.domain.CtTaskTag;
import com.bs.ct.service.ICtTaskTagService;
import javax.annotation.Resource;

/**
 * 任务标签Controller
 *
 * @author bs
 * @date 2025-02-22
 */
@Api(tags = "任务标签")
@RestController
@RequestMapping("/task/tag")
public class CtTaskTagController extends BaseController {
    @Resource
    private ICtTaskTagService ctTaskTagService;

    /**
     * 分页查询任务标签列表
     */
    @ApiOperation("分页查询任务标签列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CtTaskTag ctTaskTag) {
        startPage();
        LambdaQueryWrapper<CtTaskTag> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskTag);
        List<CtTaskTag> list = ctTaskTagService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询任务标签列表
     */
    @ApiOperation("查询任务标签列表")
    @GetMapping("/list")
    public AjaxResult list(CtTaskTag ctTaskTag) {
        LambdaQueryWrapper<CtTaskTag> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskTag);
        List<CtTaskTag> list = ctTaskTagService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出任务标签列表
     */
    @ApiOperation("导出任务标签列表")
    @Log(title = "任务标签导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CtTaskTag ctTaskTag) {
        LambdaQueryWrapper<CtTaskTag> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTaskTag);
        List<CtTaskTag> list = ctTaskTagService.list(queryWrapper);
        ExcelUtil<CtTaskTag> util = new ExcelUtil<CtTaskTag>(CtTaskTag. class);
        util.exportExcel(response, list, "任务标签数据");
    }

    /**
     * 获取任务标签详细信息
     */
    @ApiOperation("获取任务标签详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ctTaskTagService.getById(id));
    }

    /**
     * 新增任务标签
     */
    @ApiOperation("新增任务标签")
    @Log(title = "任务标签新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CtTaskTag ctTaskTag) {
        return toAjax(ctTaskTagService.save(ctTaskTag));
    }

    /**
     * 修改任务标签
     */
    @ApiOperation("修改任务标签")
    @Log(title = "任务标签修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CtTaskTag ctTaskTag) {
        return toAjax(ctTaskTagService.updateById(ctTaskTag));
    }

    /**
     * 删除任务标签
     */
    @ApiOperation("删除任务标签")
    @Log(title = "任务标签删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(ctTaskTagService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CtTaskTag> queryWrapper,CtTaskTag ctTaskTag){


        //id
        if(Validator.isNotEmpty(ctTaskTag.getId())){
            queryWrapper.eq(CtTaskTag::getId,ctTaskTag.getId());
        }

        //任务模板id
        if(Validator.isNotEmpty(ctTaskTag.getTaskId())){
            queryWrapper.eq(CtTaskTag::getTaskId,ctTaskTag.getTaskId());
        }

        //标签名称
        if(Validator.isNotEmpty(ctTaskTag.getTagName())){
            queryWrapper.eq(CtTaskTag::getTagName,ctTaskTag.getTagName());
        }

        //标签说明
        if(Validator.isNotEmpty(ctTaskTag.getTagDesc())){
            queryWrapper.eq(CtTaskTag::getTagDesc,ctTaskTag.getTagDesc());
        }

        //是否需要拍照
        if(Validator.isNotEmpty(ctTaskTag.getIsPhoto())){
            queryWrapper.eq(CtTaskTag::getIsPhoto,ctTaskTag.getIsPhoto());
        }

        //拍照数量要求
        if(Validator.isNotEmpty(ctTaskTag.getPhotoNum())){
            queryWrapper.eq(CtTaskTag::getPhotoNum,ctTaskTag.getPhotoNum());
        }

        //照片存放目录
        if(Validator.isNotEmpty(ctTaskTag.getSaveDir())){
            queryWrapper.eq(CtTaskTag::getSaveDir,ctTaskTag.getSaveDir());
        }

        //附件id
        if(Validator.isNotEmpty(ctTaskTag.getFileId())){
            queryWrapper.eq(CtTaskTag::getFileId,ctTaskTag.getFileId());
        }

        //备注
        if(Validator.isNotEmpty(ctTaskTag.getRemarks())){
            queryWrapper.eq(CtTaskTag::getRemarks,ctTaskTag.getRemarks());
        }

        //删除标志（0代表存在 2代表删除）
        if(Validator.isNotEmpty(ctTaskTag.getDelFlag())){
            queryWrapper.eq(CtTaskTag::getDelFlag,ctTaskTag.getDelFlag());
        }

        //创建部门
        if(Validator.isNotEmpty(ctTaskTag.getCreateDept())){
            queryWrapper.eq(CtTaskTag::getCreateDept,ctTaskTag.getCreateDept());
        }

        //创建人
        if(Validator.isNotEmpty(ctTaskTag.getCreateBy())){
            queryWrapper.eq(CtTaskTag::getCreateBy,ctTaskTag.getCreateBy());
        }

        //创建时间
        if(Validator.isNotEmpty(ctTaskTag.getCreateTime())){
            queryWrapper.eq(CtTaskTag::getCreateTime,ctTaskTag.getCreateTime());
        }

        //修改人
        if(Validator.isNotEmpty(ctTaskTag.getUpdateBy())){
            queryWrapper.eq(CtTaskTag::getUpdateBy,ctTaskTag.getUpdateBy());
        }

        //修改时间
        if(Validator.isNotEmpty(ctTaskTag.getUpdateTime())){
            queryWrapper.eq(CtTaskTag::getUpdateTime,ctTaskTag.getUpdateTime());
        }

        //租户ID
        if(Validator.isNotEmpty(ctTaskTag.getTenantId())){
            queryWrapper.eq(CtTaskTag::getTenantId,ctTaskTag.getTenantId());
        }

        }

}
