package com.bs.ct.controller;

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

import com.bs.cm.domain.CmAttach;
import com.bs.cm.service.ICmAttachService;
import com.bs.common.exception.ServiceException;
import com.bs.ct.domain.CtTaskInfo;
import com.bs.ct.domain.CtTaskTemplateTag;
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.CtTag;
import com.bs.ct.service.ICtTagService;
import javax.annotation.Resource;

/**
 * 标签信息Controller
 *
 * @author bs
 * @date 2025-02-22
 */
@Api(tags = "标签信息")
@RestController
@RequestMapping("/system/tag")
public class CtTagController extends BaseController {
    @Resource
    private ICtTagService ctTagService;
    @Resource
    private ICmAttachService cmAttachService;
    /**
     * 分页查询标签信息列表
     */
    @ApiOperation("分页查询标签信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CtTag ctTag) {
        startPage();
        LambdaQueryWrapper<CtTag> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTag);
        List<CtTag> list = ctTagService.list(queryWrapper);
        for (CtTag tag : list) {
            List<CmAttach> cmAttaches = cmAttachService.list(new LambdaQueryWrapper<CmAttach>().eq(CmAttach::getFileId, tag.getFileId()));
            if (null != cmAttaches && cmAttaches.size() > 0) {
                tag.setFiles(cmAttaches);
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询标签信息列表
     */
    @ApiOperation("查询标签信息列表")
    @GetMapping("/list")
    public AjaxResult list(CtTag ctTag) {
        LambdaQueryWrapper<CtTag> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctTag);
        List<CtTag> list = ctTagService.list(queryWrapper);
        return success(list);
    }

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

    /**
     * 获取标签信息详细信息
     */
    @ApiOperation("获取标签信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        CtTag ctTag = ctTagService.getById(id);
        List<CmAttach> cmAttaches = cmAttachService.list(new LambdaQueryWrapper<CmAttach>()
                .eq(CmAttach::getFileId, ctTag.getFileId()));
        ctTag.setFiles(cmAttaches);
        return success(ctTag);
    }

    /**
     * 新增标签信息
     */
    @ApiOperation("新增标签信息")
    @Log(title = "标签信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CtTag ctTag) {
        List<CtTag> ctTags = ctTagService.list(new LambdaQueryWrapper<CtTag>().eq(CtTag::getTagName, ctTag.getTagName()));
        if (null != ctTags && ctTags.size() > 0) {
            throw new ServiceException("该标签名称已经存在");
        }
        updateFile(ctTag);
        return toAjax(ctTagService.save(ctTag));
    }

    private void updateFile(CtTag ctTag) {
        Long newId = System.currentTimeMillis() + new Random().nextInt(1000);
        List<CmAttach> file = ctTag.getFiles();
        if (null != file) {
            for (CmAttach cmAttach : file) {
                cmAttach.setFileId(String.valueOf(newId));
            }
            cmAttachService.updateBatchById(file);
            ctTag.setFileId(newId);
        }
    }

    /**
     * 修改标签信息
     */
    @ApiOperation("修改标签信息")
    @Log(title = "标签信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CtTag ctTag) {
        updateFile(ctTag);
        return toAjax(ctTagService.updateById(ctTag));
    }

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

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


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

        //标签名称
        if(Validator.isNotEmpty(ctTag.getTagName())){
            queryWrapper.like(CtTag::getTagName,ctTag.getTagName());
        }

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

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

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

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

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

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

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

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

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

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

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

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

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

        }

}
