package org.jeecg.modules.okki.showlist.controller;

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.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.okki.showlist.entity.*;
import org.jeecg.modules.okki.showlist.service.*;
import org.jeecg.modules.okki.showlist.vo.OkkiShowlistPage;
import org.jeecg.modules.okki.showlist.vo.ProductListDataVO;
import org.jeecg.modules.okki.showlist.vo.ProductListVO;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
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.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

 /**
 * @Description: showlist
 * @Author: jeecg-boot
 * @Date:   2024-05-14
 * @Version: V1.0
 */
@Api(tags="showlist")
@RestController
@RequestMapping("/showlist/okkiShowlist")
@Slf4j
public class OkkiShowlistController {
	 @Autowired
	 private IOkkiShowlistService okkiShowlistService;
	 @Autowired
	 private IOkkiOverviewService okkiOverviewService;
	 @Autowired
	 private IOkkiProductListService okkiProductListService;
	 @Autowired
	 private IOkkiAdvantageService okkiAdvantageService;
	 @Autowired
	 private IOkkiRecommendedProductsService okkiRecommendedProductsService;
	 @Autowired
	 private IOkkiKeywordsService okkiKeywordsService;
	 @Autowired
	 private IOkkiFaqService okkiFaqService;
	 @Autowired
	 private IOkkiShowlistBlogService okkiShowlistBlogService;

	 /**
	  * 分页列表查询
	  *
	  * @param okkiShowlist
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "showlist-分页列表查询")
	 @ApiOperation(value="showlist-分页列表查询", notes="showlist-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<IPage<OkkiShowlist>> queryPageList(OkkiShowlist okkiShowlist,
													  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													  HttpServletRequest req) {
		 QueryWrapper<OkkiShowlist> queryWrapper = QueryGenerator.initQueryWrapper(okkiShowlist, req.getParameterMap());
		 Page<OkkiShowlist> page = new Page<OkkiShowlist>(pageNo, pageSize);
		 IPage<OkkiShowlist> pageList = okkiShowlistService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 /**
	  *   添加
	  *
	  * @param okkiShowlistPage
	  * @return
	  */
	 @AutoLog(value = "showlist-添加")
	 @ApiOperation(value="showlist-添加", notes="showlist-添加")
	 @RequiresPermissions("showlist:okki_showlist:add")
	 @PostMapping(value = "/add")
	 public Result<String> add(@RequestBody OkkiShowlistPage okkiShowlistPage) {
		 OkkiShowlist okkiShowlist = new OkkiShowlist();
		 BeanUtils.copyProperties(okkiShowlistPage, okkiShowlist);
		 okkiShowlistService.saveMain(okkiShowlist, okkiShowlistPage.getOkkiOverviewList(),okkiShowlistPage.getOkkiProductListList(),okkiShowlistPage.getOkkiAdvantageList(),okkiShowlistPage.getOkkiRecommendedProductsList(),okkiShowlistPage.getOkkiKeywordsList(),okkiShowlistPage.getOkkiFaqList(),okkiShowlistPage.getOkkiShowlistBlogList());
		 return Result.OK("添加成功！");
	 }

	 /**
	  *  编辑
	  *
	  * @param okkiShowlistPage
	  * @return
	  */
	 @AutoLog(value = "showlist-编辑")
	 @ApiOperation(value="showlist-编辑", notes="showlist-编辑")
	 @RequiresPermissions("showlist:okki_showlist:edit")
	 @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> edit(@RequestBody OkkiShowlistPage okkiShowlistPage) {
		 OkkiShowlist okkiShowlist = new OkkiShowlist();
		 BeanUtils.copyProperties(okkiShowlistPage, okkiShowlist);
		 okkiShowlistPage.setStatus(0);
		 OkkiShowlist okkiShowlistEntity = okkiShowlistService.getById(okkiShowlist.getId());
		 if(okkiShowlistEntity==null) {
			 return Result.error("未找到对应数据");
		 }
		 okkiShowlistService.updateMain(okkiShowlist, okkiShowlistPage.getOkkiOverviewList(),okkiShowlistPage.getOkkiProductListList(),okkiShowlistPage.getOkkiAdvantageList(),okkiShowlistPage.getOkkiRecommendedProductsList(),okkiShowlistPage.getOkkiKeywordsList(),okkiShowlistPage.getOkkiFaqList(),okkiShowlistPage.getOkkiShowlistBlogList());
		 return Result.OK("编辑成功!");
	 }

	 /**
	  *   通过id删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "showlist-通过id删除")
	 @ApiOperation(value="showlist-通过id删除", notes="showlist-通过id删除")
	 @RequiresPermissions("showlist:okki_showlist:delete")
	 @DeleteMapping(value = "/delete")
	 public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		 okkiShowlistService.delMain(id);
		 return Result.OK("删除成功!");
	 }

	 /**
	  *  批量删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "showlist-批量删除")
	 @ApiOperation(value="showlist-批量删除", notes="showlist-批量删除")
	 @RequiresPermissions("showlist:okki_showlist:deleteBatch")
	 @DeleteMapping(value = "/deleteBatch")
	 public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		 this.okkiShowlistService.delBatchMain(Arrays.asList(ids.split(",")));
		 return Result.OK("批量删除成功！");
	 }

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "showlist-通过id查询")
	 @ApiOperation(value="showlist-通过id查询", notes="showlist-通过id查询")
	 @GetMapping(value = "/queryById")
	 public Result<OkkiShowlist> queryById(@RequestParam(name="id",required=true) String id) {
		 OkkiShowlist okkiShowlist = okkiShowlistService.getById(id);
		 if(okkiShowlist==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(okkiShowlist);

	 }

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "概览通过主表ID查询")
	 @ApiOperation(value="概览主表ID查询", notes="概览-通主表ID查询")
	 @GetMapping(value = "/queryOkkiOverviewByMainId")
	 public Result<List<OkkiOverview>> queryOkkiOverviewListByMainId(@RequestParam(name="id",required=true) String id) {
		 List<OkkiOverview> okkiOverviewList = okkiOverviewService.selectByMainId(id);
		 return Result.OK(okkiOverviewList);
	 }
	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "产品通过主表ID查询")
	 @ApiOperation(value="产品主表ID查询", notes="产品-通主表ID查询")
	 @GetMapping(value = "/queryOkkiProductListByMainId")
	 public Result<List<OkkiProductList>> queryOkkiProductListListByMainId(@RequestParam(name="id",required=true) String id) {
		 List<OkkiProductList> okkiProductListList = okkiProductListService.selectByMainId(id);
		 return Result.OK(okkiProductListList);
	 }
	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "优势通过主表ID查询")
	 @ApiOperation(value="优势主表ID查询", notes="优势-通主表ID查询")
	 @GetMapping(value = "/queryOkkiAdvantageByMainId")
	 public Result<List<OkkiAdvantage>> queryOkkiAdvantageListByMainId(@RequestParam(name="id",required=true) String id) {
		 List<OkkiAdvantage> okkiAdvantageList = okkiAdvantageService.selectByMainId(id);
		 return Result.OK(okkiAdvantageList);
	 }
	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "推荐商品列表通过主表ID查询")
	 @ApiOperation(value="推荐商品列表主表ID查询", notes="推荐商品列表-通主表ID查询")
	 @GetMapping(value = "/queryOkkiRecommendedProductsByMainId")
	 public Result<List<OkkiRecommendedProducts>> queryOkkiRecommendedProductsListByMainId(@RequestParam(name="id",required=true) String id) {
		 List<OkkiRecommendedProducts> okkiRecommendedProductsList = okkiRecommendedProductsService.selectByMainId(id);
		 return Result.OK(okkiRecommendedProductsList);
	 }
	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "关键词通过主表ID查询")
	 @ApiOperation(value="关键词主表ID查询", notes="关键词-通主表ID查询")
	 @GetMapping(value = "/queryOkkiKeywordsByMainId")
	 public Result<List<OkkiKeywords>> queryOkkiKeywordsListByMainId(@RequestParam(name="id",required=true) String id) {
		 List<OkkiKeywords> okkiKeywordsList = okkiKeywordsService.selectByMainId(id);
		 return Result.OK(okkiKeywordsList);
	 }
	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "常见问题通过主表ID查询")
	 @ApiOperation(value="常见问题主表ID查询", notes="常见问题-通主表ID查询")
	 @GetMapping(value = "/queryOkkiFaqByMainId")
	 public Result<List<OkkiFaq>> queryOkkiFaqListByMainId(@RequestParam(name="id",required=true) String id) {
		 List<OkkiFaq> okkiFaqList = okkiFaqService.selectByMainId(id);
		 return Result.OK(okkiFaqList);
	 }
	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 //@AutoLog(value = "博客通过主表ID查询")
	 @ApiOperation(value="博客主表ID查询", notes="博客-通主表ID查询")
	 @GetMapping(value = "/queryOkkiShowlistBlogByMainId")
	 public Result<List<OkkiShowlistBlog>> queryOkkiShowlistBlogListByMainId(@RequestParam(name="id",required=true) String id) {
		 List<OkkiShowlistBlog> okkiShowlistBlogList = okkiShowlistBlogService.selectByMainId(id);
		 return Result.OK(okkiShowlistBlogList);
	 }

	 /**
	  * 导出excel
	  *
	  * @param request
	  * @param okkiShowlist
	  */
	 @RequiresPermissions("showlist:okki_showlist:exportXls")
	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(HttpServletRequest request, OkkiShowlist okkiShowlist) {
		 // Step.1 组装查询条件查询数据
		 QueryWrapper<OkkiShowlist> queryWrapper = QueryGenerator.initQueryWrapper(okkiShowlist, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 //配置选中数据查询条件
		 String selections = request.getParameter("selections");
		 if(oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 queryWrapper.in("id",selectionList);
		 }
		 //Step.2 获取导出数据
		 List<OkkiShowlist> okkiShowlistList = okkiShowlistService.list(queryWrapper);

		 // Step.3 组装pageList
		 List<OkkiShowlistPage> pageList = new ArrayList<OkkiShowlistPage>();
		 for (OkkiShowlist main : okkiShowlistList) {
			 OkkiShowlistPage vo = new OkkiShowlistPage();
			 BeanUtils.copyProperties(main, vo);
			 List<OkkiOverview> okkiOverviewList = okkiOverviewService.selectByMainId(main.getId());
			 vo.setOkkiOverviewList(okkiOverviewList);
			 List<OkkiProductList> okkiProductListList = okkiProductListService.selectByMainId(main.getId());
			 vo.setOkkiProductListList(okkiProductListList);
			 List<OkkiAdvantage> okkiAdvantageList = okkiAdvantageService.selectByMainId(main.getId());
			 vo.setOkkiAdvantageList(okkiAdvantageList);
			 List<OkkiRecommendedProducts> okkiRecommendedProductsList = okkiRecommendedProductsService.selectByMainId(main.getId());
			 vo.setOkkiRecommendedProductsList(okkiRecommendedProductsList);
			 List<OkkiKeywords> okkiKeywordsList = okkiKeywordsService.selectByMainId(main.getId());
			 vo.setOkkiKeywordsList(okkiKeywordsList);
			 List<OkkiFaq> okkiFaqList = okkiFaqService.selectByMainId(main.getId());
			 vo.setOkkiFaqList(okkiFaqList);
			 List<OkkiShowlistBlog> okkiShowlistBlogList = okkiShowlistBlogService.selectByMainId(main.getId());
			 vo.setOkkiShowlistBlogList(okkiShowlistBlogList);
			 pageList.add(vo);
		 }

		 // Step.4 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.FILE_NAME, "showlist列表");
		 mv.addObject(NormalExcelConstants.CLASS, OkkiShowlistPage.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("showlist数据", "导出人:"+sysUser.getRealname(), "showlist"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
		 return mv;
	 }

	 /**
	  * 通过excel导入数据
	  *
	  * @param request
	  * @param response
	  * @return
	  */
	 @RequiresPermissions("showlist:okki_showlist:importExcel")
	 @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	 public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			 // 获取上传文件对象
			 MultipartFile file = entity.getValue();
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<OkkiShowlistPage> list = ExcelImportUtil.importExcel(file.getInputStream(), OkkiShowlistPage.class, params);
				 for (OkkiShowlistPage page : list) {
					 OkkiShowlist po = new OkkiShowlist();
					 BeanUtils.copyProperties(page, po);
					 okkiShowlistService.saveMain(po, page.getOkkiOverviewList(),page.getOkkiProductListList(),page.getOkkiAdvantageList(),page.getOkkiRecommendedProductsList(),page.getOkkiKeywordsList(),page.getOkkiFaqList(),page.getOkkiShowlistBlogList());
				 }
				 return Result.OK("文件导入成功！数据行数:" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(),e);
				 return Result.error("文件导入失败:"+e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.OK("文件导入失败！");
	 }


	 /**
	  * 变更状态
	  * @param okkiShowlist
	  * @return
	  */
	 @PostMapping(value = "/status")
	 public Result<String> changeStatus(@RequestBody OkkiShowlist okkiShowlist, HttpServletRequest request) throws Exception {
		 String scheme = request.getScheme();             // http
		 String serverName = request.getServerName();     // hostname.com
		 int serverPort = request.getServerPort();
		 String domain = scheme + "://" +serverName + ":" + serverPort;
		 boolean result = okkiShowlistService.changeStatus(okkiShowlist, domain);
		 if (result) {
			 return Result.OK("操作成功");
		 }else {
			 return Result.error("操作失败");
		 }
	 }

	 @GetMapping(value = "productList")
	 public Result<ProductListVO> productList(@RequestParam(name="siteId",required=true) String siteId) throws Exception {
		 final ProductListVO productList = okkiShowlistService.getProductList(siteId);
		 return Result.OK(productList);
	 }
}
