package com.ruoyi.web.controller.system;

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

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ClewImg;
import com.ruoyi.system.service.IClewImgService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 线索图片Controller
 *
 * @author ruoyi
 * @date 2023-11-06
 */
@Controller
@RequestMapping("/system/img")
public class ClewImgController extends BaseController
{
    private String prefix = "system/img";

    @Autowired
    private IClewImgService clewImgService;

    @RequiresPermissions("system:img:view")
    @GetMapping()
    public String img()
    {
        return prefix + "/img";
    }

    /**
     * 查询线索图片列表
     */
    @RequiresPermissions("system:img:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ClewImg clewImg)
    {
        startPage();
        List<ClewImg> list = clewImgService.selectClewImgList(clewImg);
        return getDataTable(list);
    }

    @GetMapping("/app/list")
    @ResponseBody
    public AjaxResult getAppList(@RequestParam(value = "sourceApp", required = false) Long sourceApp)
    {
        if(sourceApp == null){
            sourceApp = 1L;
        }
        ClewImg param = new ClewImg();
        param.setSourceApp(sourceApp);
        List<ClewImg> list = clewImgService.selectClewImgList(param);
        List<String> imgList = list.stream().map(ClewImg::getImgUrl).collect(Collectors.toList());
        return AjaxResult.success(imgList);
    }

    /**
     * 导出线索图片列表
     */
    @RequiresPermissions("system:img:export")
    @Log(title = "线索图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ClewImg clewImg)
    {
        List<ClewImg> list = clewImgService.selectClewImgList(clewImg);
        ExcelUtil<ClewImg> util = new ExcelUtil<ClewImg>(ClewImg.class);
        return util.exportExcel(list, "线索图片数据");
    }

    /**
     * 新增线索图片
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存线索图片
     */
    @RequiresPermissions("system:img:add")
    @Log(title = "线索图片", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ClewImg clewImg)
    {
        return toAjax(clewImgService.insertClewImg(clewImg));
    }

    /**
     * 修改线索图片
     */
    @RequiresPermissions("system:img:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ClewImg clewImg = clewImgService.selectClewImgById(id);
        mmap.put("clewImg", clewImg);
        return prefix + "/edit";
    }

    /**
     * 修改保存线索图片
     */
    @RequiresPermissions("system:img:edit")
    @Log(title = "线索图片", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ClewImg clewImg)
    {
        return toAjax(clewImgService.updateClewImg(clewImg));
    }

    /**
     * 删除线索图片
     */
    @RequiresPermissions("system:img:remove")
    @Log(title = "线索图片", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(clewImgService.deleteClewImgByIds(ids));
    }
}
