package com.fs.company.controller.qw;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.qw.domain.QwAutoTagsLogs;
import com.fs.qw.param.QwAutoTagsLogsParams;
import com.fs.qw.param.QwTagSearchParam;
import com.fs.qw.service.IQwAutoTagsLogsService;
import com.fs.qw.service.IQwTagService;
import com.fs.qw.vo.QwAutoTagsLogsVO;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 自动打标签的日志Controller
 *
 * @author fs
 * @date 2024-07-20
 */
@RestController
@RequestMapping("/qw/autoTagsLogs")
public class QwAutoTagsLogsController extends BaseController
{
    @Autowired
    private IQwAutoTagsLogsService qwAutoTagsLogsService;


    @Autowired
    private TokenService tokenService;

    @Autowired
    private IQwTagService iQwTagService;

    /**
     * 查询自动打标签的日志列表
     */
    @PreAuthorize("@ss.hasPermi('qw:autoTags:query')")
    @GetMapping("/list")
    public TableDataInfo list(QwAutoTagsLogsParams params)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        params.setCompanyId( loginUser.getCompany().getCompanyId());
        List<QwAutoTagsLogsVO> list = qwAutoTagsLogsService.selectQwAutoTagsLogsListVO(params);
        list.forEach(item->{

            if (!Objects.equals(item.getEffectiveRules(), "[]") && item.getEffectiveRules()!=null) {
                QwTagSearchParam param = new QwTagSearchParam();
                Gson gson = new Gson();
                List<String> tagIds = gson.fromJson(
                        item.getEffectiveRules(),
                        new TypeToken<List<String>>() {
                        }.getType()
                );

                param.setTagIds(tagIds);

                item.setTagIdsName(iQwTagService.selectQwTagListByTagIds(param));
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出自动打标签的日志列表
     */
    @PreAuthorize("@ss.hasPermi('qw:autoTags:export')")
    @Log(title = "自动打标签的日志", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwAutoTagsLogs qwAutoTagsLogs)
    {
        List<QwAutoTagsLogs> list = qwAutoTagsLogsService.selectQwAutoTagsLogsList(qwAutoTagsLogs);
        ExcelUtil<QwAutoTagsLogs> util = new ExcelUtil<QwAutoTagsLogs>(QwAutoTagsLogs.class);
        return util.exportExcel(list, "自动打标签的日志数据");
    }

    /**
     * 获取自动打标签的日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:autoTags:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwAutoTagsLogsService.selectQwAutoTagsLogsById(id));
    }

    /**
     * 新增自动打标签的日志
     */
    @PreAuthorize("@ss.hasPermi('qw:autoTags:add')")
    @Log(title = "自动打标签的日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwAutoTagsLogs qwAutoTagsLogs)
    {
        return toAjax(qwAutoTagsLogsService.insertQwAutoTagsLogs(qwAutoTagsLogs));
    }

    /**
     * 修改自动打标签的日志
     */
    @PreAuthorize("@ss.hasPermi('qw:autoTags:edit')")
    @Log(title = "自动打标签的日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwAutoTagsLogs qwAutoTagsLogs)
    {
        return toAjax(qwAutoTagsLogsService.updateQwAutoTagsLogs(qwAutoTagsLogs));
    }

    /**
     * 删除自动打标签的日志
     */
    @PreAuthorize("@ss.hasPermi('qw:autoTags:remove')")
    @Log(title = "自动打标签的日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwAutoTagsLogsService.deleteQwAutoTagsLogsByIds(ids));
    }
}
