package com.willalp.web.controller.clockingin;

import com.willalp.common.annotation.Log;
import com.willalp.common.config.WillalpConfig;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.file.FileUploadUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.event.domain.HsEventImg;
import com.willalp.event.service.IHsEventImgService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * 事件（活动）图片信息Controller
 *
 * @author willalp
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/event/img")
public class HsEventImgController extends BaseController {
    @Resource
    private IHsEventImgService hsEventImgService;

    /**
     * 查询事件（活动）图片信息列表
     */
    @PreAuthorize("@ss.hasPermi('event:img:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsEventImg hsEventImg) {
        startPage();
        List<HsEventImg> list = hsEventImgService.selectHsEventImgList(hsEventImg);
        return getDataTable(list);
    }

    /**
     * 导出事件（活动）图片信息列表
     */
    @PreAuthorize("@ss.hasPermi('event:img:export')")
    @Log(title = "事件（活动）图片信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsEventImg hsEventImg) {
        List<HsEventImg> list = hsEventImgService.selectHsEventImgList(hsEventImg);
        ExcelUtil<HsEventImg> util = new ExcelUtil<HsEventImg>(HsEventImg.class);
        return util.exportExcel(list, "事件（活动）图片信息数据");
    }

    /**
     * 获取事件（活动）图片信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('event:img:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(hsEventImgService.getById(id));
    }

    @Value("${imgServer.url}")
    public String baseUrl;

    /**
     * 活动风采上传
     */
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file, HttpServletRequest request) throws Exception {
        try {
            // 上传文件路径
            String filePath = WillalpConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = file.getOriginalFilename();
            String url = FileUploadUtils.upload(filePath, file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", baseUrl + url);
            ajax.put("type", fileName.split("\\.")[1]);
            ajax.put("eventId", request.getHeader("eventId"));
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增事件（活动）图片信息
     */
    @PreAuthorize("@ss.hasPermi('event:img:add')")
    @Log(title = "事件（活动）图片信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsEventImg hsEventImg) {
        hsEventImg.setCreateBy(SecurityUtils.getUsername());
        hsEventImg.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsEventImgService.save(hsEventImg));
    }

    /**
     * 修改事件（活动）图片信息
     */
    @PreAuthorize("@ss.hasPermi('event:img:edit')")
    @Log(title = "事件（活动）图片信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsEventImg hsEventImg) {
        hsEventImg.setUpdateBy(SecurityUtils.getUsername());
        hsEventImg.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsEventImgService.updateById(hsEventImg));
    }

    /**
     * 删除事件（活动）图片信息
     */
    @PreAuthorize("@ss.hasPermi('event:img:remove')")
    @Log(title = "事件（活动）图片信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(hsEventImgService.removeByIds(Arrays.asList(ids)));
    }
}
