package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TPersonalImg;
import com.ruoyi.system.service.ITPersonalImgService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 个人页 图片Controller
 * 
 * @author Alex
 * @date 2020-09-16
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/personalImg" )
public class TPersonalImgController extends BaseController {

    private final ITPersonalImgService iTPersonalImgService;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询个人页 图片列表
     */
    @PreAuthorize("@ss.hasPermi('system:img:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPersonalImg tPersonalImg)
    {
        startPage();
        LambdaQueryWrapper<TPersonalImg> lqw = new LambdaQueryWrapper<TPersonalImg>();
        if (tPersonalImg.getPersonalId() != null){
            lqw.eq(TPersonalImg::getPersonalId ,tPersonalImg.getPersonalId());
        }
        if (StringUtils.isNotBlank(tPersonalImg.getUrl())){
            lqw.eq(TPersonalImg::getUrl ,tPersonalImg.getUrl());
        }
        if (StringUtils.isNotBlank(tPersonalImg.getEnable())){
            lqw.eq(TPersonalImg::getEnable ,tPersonalImg.getEnable());
        }
        List<TPersonalImg> list = iTPersonalImgService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 查询个人页 图片列表
     */
    @ApiOperation("个人图片列表")
    @GetMapping("/all")
    public AjaxResult all(TPersonalImg tPersonalImg)
    {
        List<TPersonalImg> list = iTPersonalImgService.list(new QueryWrapper<TPersonalImg>()
                .eq(StringUtils.isNotBlank(tPersonalImg.getEnable()),"enable", tPersonalImg.getEnable())
        );
        return AjaxResult.success(list);
    }
    /**
     * 导出个人页 图片列表
     */
    @PreAuthorize("@ss.hasPermi('system:img:export')" )
    @Log(title = "个人页 图片" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(TPersonalImg tPersonalImg) {
        LambdaQueryWrapper<TPersonalImg> lqw = new LambdaQueryWrapper<TPersonalImg>(tPersonalImg);
        List<TPersonalImg> list = iTPersonalImgService.list(lqw);
        ExcelUtil<TPersonalImg> util = new ExcelUtil<TPersonalImg>(TPersonalImg. class);
        return util.exportExcel(list, "img" );
    }

    /**
     * 获取个人页 图片详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:img:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iTPersonalImgService.getById(id));
    }

    /**
     * 新增个人页 图片
     */
    @PreAuthorize("@ss.hasPermi('system:img:add')" )
    @Log(title = "个人页 图片" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPersonalImg tPersonalImg) {
        return toAjax(iTPersonalImgService.save(tPersonalImg) ? 1 : 0);
    }

    /**
     * 修改个人页 图片
     */
    @PreAuthorize("@ss.hasPermi('system:img:edit')" )
    @Log(title = "个人页 图片" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPersonalImg tPersonalImg) {
        return toAjax(iTPersonalImgService.updateById(tPersonalImg) ? 1 : 0);
    }

    /**
     * 删除个人页 图片
     */
    @PreAuthorize("@ss.hasPermi('system:img:remove')" )
    @Log(title = "个人页 图片" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iTPersonalImgService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file, String personalId) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getProfile() + "/personal";
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);

            TPersonalImg item = new TPersonalImg();
            item.setPersonalId(Long.parseLong(personalId));
            item.setEnable("0");
            item.setUrl(fileName);
            iTPersonalImgService.save(item);

            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}