package org.jeecg.modules.logo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.modules.logo.entity.CsPtLogo;
import org.jeecg.modules.logo.service.ICsPtLogoService;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;

/**
 * @Description: 系统logo设置
 * @Author: jeecg-boot
 * @Date:   2023-12-26
 * @Version: V1.0
 */
@Api(tags="系统logo设置")
@RestController
@RequestMapping("/logo/csPtLogo")
@Slf4j
public class CsPtLogoController extends JeecgController<CsPtLogo, ICsPtLogoService> {
	@Autowired
	private ICsPtLogoService csPtLogoService;
	
	/**
	 * 分页列表查询
	 *
	 * @param csPtLogo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "系统logo设置-分页列表查询")
	@ApiOperation(value="系统logo设置-分页列表查询", notes="系统logo设置-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CsPtLogo>> queryPageList(CsPtLogo csPtLogo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CsPtLogo> queryWrapper = QueryGenerator.initQueryWrapper(csPtLogo, req.getParameterMap());
		Page<CsPtLogo> page = new Page<CsPtLogo>(pageNo, pageSize);
		IPage<CsPtLogo> pageList = csPtLogoService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@ApiOperation(value="系统logo设置-上传文件", notes="系统logo设置-上传文件")
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public Result<?> uploadFile(HttpServletRequest request, HttpServletResponse response) {
		String xtmc = request.getParameter("xtmc");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		String tenantId = TenantContext.getTenant();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			// 获取上传文件对象
			MultipartFile file = entity.getValue();
			ImportParams params = new ImportParams();
			params.setTitleRows(1);
			params.setHeadRows(1);
			params.setNeedSave(true);

			try {
				String fuleSuffix  = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
				InputStream inputStream = file.getInputStream();
				//上传minio
				String relativePath = "logo/" + TenantContext.getTenant()  + "/" + IdWorker.getIdStr() + fuleSuffix;
				String fileUrl = CommonUtils.upload(inputStream, relativePath);

				CsPtLogo logo = csPtLogoService.lambdaQuery().eq(CsPtLogo::getTenantId,tenantId).one();
				if(logo == null) {
					logo = new CsPtLogo();
					logo.setId(IdWorker.getIdStr());
				}
				logo.setXtmc(xtmc);
				logo.setLogourl(fileUrl);
				csPtLogoService.saveOrUpdate(logo);
			}catch (Exception e){
				log.error(e.getMessage(),e);
				return Result.error(e.getMessage());
			}
		}

		return Result.OK("保存成功");
	}

	/**
	 *   添加
	 *
	 * @param csPtLogo
	 * @return
	 */
	@AutoLog(value = "系统logo设置-添加")
	@ApiOperation(value="系统logo设置-添加", notes="系统logo设置-添加")
	@RequiresPermissions("logo:cs_pt_logo:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CsPtLogo csPtLogo) {
		csPtLogoService.save(csPtLogo);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param csPtLogo
	 * @return
	 */
	@AutoLog(value = "系统logo设置-编辑")
	@ApiOperation(value="系统logo设置-编辑", notes="系统logo设置-编辑")
	@RequiresPermissions("logo:cs_pt_logo:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CsPtLogo csPtLogo) {
		csPtLogoService.updateById(csPtLogo);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "系统logo设置-通过id删除")
	@ApiOperation(value="系统logo设置-通过id删除", notes="系统logo设置-通过id删除")
	@RequiresPermissions("logo:cs_pt_logo:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		csPtLogoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "系统logo设置-批量删除")
	@ApiOperation(value="系统logo设置-批量删除", notes="系统logo设置-批量删除")
	@RequiresPermissions("logo:cs_pt_logo:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.csPtLogoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "系统logo设置-通过id查询")
	@ApiOperation(value="系统logo设置-通过id查询", notes="系统logo设置-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CsPtLogo> queryById(@RequestParam(name="id",required=true) String id) {
		CsPtLogo csPtLogo = csPtLogoService.getById(id);
		if(csPtLogo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(csPtLogo);
	}

	/**
	 * 查询当前租户logo设置
	 *
	 * @return
	 */
	//@AutoLog(value = "系统logo设置-通过id查询")
	@ApiOperation(value="系统logo设置-查询租户logo", notes="系统logo设置-查询租户logo")
	@GetMapping(value = "/querySysLogo")
	public Result<CsPtLogo> querySysLogo() {
		String tenantId = TenantContext.getTenant();
		CsPtLogo csPtLogo = csPtLogoService.lambdaQuery().eq(CsPtLogo::getTenantId,tenantId).one();
		if(csPtLogo == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(csPtLogo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param csPtLogo
    */
    @RequiresPermissions("logo:cs_pt_logo:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CsPtLogo csPtLogo) {
        return super.exportXls(request, csPtLogo, CsPtLogo.class, "系统logo设置");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("logo:cs_pt_logo:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CsPtLogo.class);
    }

}
