package org.jeecg.modules.demo.ggzyannouncementtype.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.ggzyannouncementtype.entity.GgzyAnnouncementType;
import org.jeecg.modules.demo.ggzyannouncementtype.service.IGgzyAnnouncementTypeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.ggzyregionalcodemapping.entity.GGzyLabelValue;
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.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
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 com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 公告类型
 * @Author: jeecg-boot
 * @Date:   2024-01-08
 * @Version: V1.0
 */
@Api(tags="公告类型")
@RestController
@RequestMapping("/ggzyannouncementtype/ggzyAnnouncementType")
@Slf4j
public class GgzyAnnouncementTypeController extends JeecgController<GgzyAnnouncementType, IGgzyAnnouncementTypeService> {
	@Autowired
	private IGgzyAnnouncementTypeService ggzyAnnouncementTypeService;
	
	/**
	 * 分页列表查询
	 *
	 * @param ggzyAnnouncementType
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 *
	 */
	//@AutoLog(value = "公告类型-分页列表查询")
	@ApiOperation(value="公告类型-分页列表查询", notes="公告类型-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<GgzyAnnouncementType>> queryPageList(GgzyAnnouncementType ggzyAnnouncementType,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GgzyAnnouncementType> queryWrapper = QueryGenerator.initQueryWrapper(ggzyAnnouncementType, req.getParameterMap());
		Page<GgzyAnnouncementType> page = new Page<GgzyAnnouncementType>(pageNo, pageSize);
		IPage<GgzyAnnouncementType> pageList = ggzyAnnouncementTypeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 @ApiOperation(value="公告类型-分页列表查询", notes="公告类型-分页列表查询")
	 @GetMapping(value = "/listGroup")
	 public Result<List<GGzyLabelValue>> queryPageListGroup(GgzyAnnouncementType ggzyAnnouncementType,
															@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
															@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
															HttpServletRequest req) {
		 List<GGzyLabelValue> labelValues = new ArrayList<GGzyLabelValue>();
		 QueryWrapper<GgzyAnnouncementType> ggzyAnnouncementTypeQueryWrapper = new QueryWrapper<GgzyAnnouncementType>();
		 ggzyAnnouncementTypeQueryWrapper.groupBy("transaction_type_name");
		 List<GgzyAnnouncementType> list = ggzyAnnouncementTypeService.list(ggzyAnnouncementTypeQueryWrapper);
		 for (GgzyAnnouncementType announcementType : list) {
			 String transactionTypeName = announcementType.getTransactionTypeName();
			 if(!ObjectUtils.isEmpty(transactionTypeName)){
				 GGzyLabelValue gGzyLabelValue = new GGzyLabelValue();
				 gGzyLabelValue.setValue(transactionTypeName);
				 gGzyLabelValue.setLabel(transactionTypeName);
				 labelValues.add(gGzyLabelValue);
			 }
		 }
		 return Result.OK(labelValues);
	 }

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

    /**
    * 导出excel
    *
    * @param request
    * @param ggzyAnnouncementType
    */
    @RequiresPermissions("ggzyannouncementtype:ggzy_announcement_type:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GgzyAnnouncementType ggzyAnnouncementType) {
        return super.exportXls(request, ggzyAnnouncementType, GgzyAnnouncementType.class, "公告类型");
    }

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

}
