package org.jeecg.modules.wps.controller;

import cn.com.ctop.common.module.constant.CtopRoleCodeConstant;
import cn.com.ctop.common.module.service.ISysRoleExtService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.wps.base.Response;
import org.jeecg.modules.wps.entity.WpsFile;
import org.jeecg.modules.wps.service.IWpsFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

/**
 * wps文件
 * @author jeecg-boot
 * @date   2020-06-01
 * @version V1.0
 */
@Slf4j
@Api(tags="wps文件")
@RestController
@RequestMapping("/ctop/wpsFile")
public class WpsFileController {
	@Autowired
	private IWpsFileService wpsFileService;
	@Autowired
	private ISysRoleExtService sysRoleService;
	@PostMapping("bindProject")
	public Map<String,Object> bindProject(String fileId,Long projectId){
		return wpsFileService.bindProject(fileId,projectId);
	}

	/**
	 * 上传文件
	 */
	@PostMapping("uploadFile")
	public ResponseEntity<Object> uploadFile(@RequestParam("file") MultipartFile file){
		try {
			LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			wpsFileService.uploadFile(file,user.getId());
			return Response.success(true,"上传成功");
		}catch (Exception e){
			e.printStackTrace();
			return Response.success(false,"上传失败");
		}
	}
	/**
	 * 分页列表查询
	 * @param wpsFile
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="wps文件-分页列表查询", notes="wps文件-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<WpsFile>> queryPageList(WpsFile wpsFile,
												@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												HttpServletRequest req) {
		String name = wpsFile.getName();
		wpsFile.setName(null);
		Result<IPage<WpsFile>> result = new Result<>();
		QueryWrapper<WpsFile> queryWrapper = QueryGenerator.initQueryWrapper(wpsFile, req.getParameterMap());
		if(null!=name&&!"".equals(name.trim())){
			queryWrapper.like("name",name);
		}
		//只查询题库的文件
		queryWrapper.eq("is_script",0);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String roleCode = sysRoleService.getRoleCodeByUserId(sysUser.getId());
		if(null==roleCode||!roleCode.equals(CtopRoleCodeConstant.COMMON_ROLE_CODE_ADMIN)){
			queryWrapper.eq("creator",sysUser.getId());
		}
		Page<WpsFile> page = new Page<>(pageNo, pageSize);
		IPage<WpsFile> pageList = wpsFileService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	/**
	 *   添加
	 * @param wpsFile
	 * @return
	 */
	@ApiOperation(value="wps文件-添加", notes="wps文件-添加")
	@PostMapping(value = "/add")
	public Result<WpsFile> add(@RequestBody WpsFile wpsFile) {
		Result<WpsFile> result = new Result<>();
		try {
			wpsFileService.save(wpsFile);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}

	/**
	 *  编辑
	 * @param wpsFile
	 * @return
	 */
	@ApiOperation(value="wps文件-编辑", notes="wps文件-编辑")
	@PutMapping(value = "/edit")
	public Result<WpsFile> edit(@RequestBody WpsFile wpsFile) {
		Result<WpsFile> result = new Result<>();
		WpsFile wpsFileEntity = wpsFileService.getById(wpsFile.getId());
		if(wpsFileEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = wpsFileService.updateById(wpsFile);
			if(ok) {
				result.success("修改成功!");
			}
		}

		return result;
	}

	/**
	 *   通过id删除
	 * @param id
	 * @return
	 */
	@ApiOperation(value="wps文件-通过id删除", notes="wps文件-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<Object> delete(@RequestParam(name="id",required=true) String id) {
		try {
			wpsFileService.removeById(id);
		} catch (Exception e) {
			log.error("删除失败",e.getMessage());
			return Result.error("删除失败!");
		}
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="wps文件-批量删除", notes="wps文件-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<WpsFile> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<WpsFile> result = new Result<>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.wpsFileService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}

	/**
	 * 通过id查询
	 * @param id
	 * @return
	 */
	@ApiOperation(value="wps文件-通过id查询", notes="wps文件-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<WpsFile> queryById(@RequestParam(name="id",required=true) String id) {
		Result<WpsFile> result = new Result<>();
		WpsFile wpsFile = wpsFileService.getById(id);
		if(wpsFile==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(wpsFile);
			result.setSuccess(true);
		}
		return result;
	}

	/**
	 * 通过wps官方模版新建文件
	 * template值 {"word", "excel", "ppt"}
	 */
	@GetMapping("createTemplateFile")
	public ResponseEntity<Object> createTemplateFile(String template){
		String newUrl = wpsFileService.createTemplateFile(template);
		return Response.success(newUrl);
	}

	/**
	 *  查询题库文件
	 */
	@GetMapping(value = "/queryQuestionBank")
	public Result<IPage<WpsFile>> queryQuestionBank(WpsFile wpsFile,
													@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													HttpServletRequest req) {
		String name = wpsFile.getName();
		wpsFile.setName(null);
		Result<IPage<WpsFile>> result = new Result<>();
		QueryWrapper<WpsFile> queryWrapper = QueryGenerator.initQueryWrapper(wpsFile, req.getParameterMap());
		if(null!=name&&!"".equals(name.trim())){
			queryWrapper.like("name",name);
		}
		//只查询题库的文件
		queryWrapper.eq("is_script",1);
		Page<WpsFile> page = new Page<>(pageNo, pageSize);
		IPage<WpsFile> pageList = wpsFileService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

}
