package org.stm.baosong.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.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.stm.baosong.entity.BaosongTplItem;
import org.stm.baosong.service.IBaosongTplItemService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 报送模板项
 * @Author: jeecg-boot
 * @Date:   2024-12-14
 * @Version: V1.0
 */
@Api(tags="报送模板项")
@RestController
@RequestMapping("/baosong/baosongTplItem")
@Slf4j
public class BaosongTplItemController extends JeecgController<BaosongTplItem, IBaosongTplItemService> {
	@Autowired
	private IBaosongTplItemService baosongTplItemService;

	//@AutoLog(value = "报送模板项-分页列表查询")
	@ApiOperation(value="报送模板项-分页列表查询", notes="报送模板项-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BaosongTplItem>> queryPageList(BaosongTplItem baosongTplItem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BaosongTplItem> queryWrapper = QueryGenerator.initQueryWrapper(baosongTplItem, req.getParameterMap());
		Page<BaosongTplItem> page = new Page<BaosongTplItem>(pageNo, pageSize);
		IPage<BaosongTplItem> pageList = baosongTplItemService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/allTplItems")
	 public Result<List<BaosongTplItem>> allTplItems(BaosongTplItem baosongTplItem, HttpServletRequest req) {
		 QueryWrapper<BaosongTplItem> queryWrapper = QueryGenerator.initQueryWrapper(baosongTplItem, req.getParameterMap());
		 queryWrapper.orderByAsc("pcode");
		 List<BaosongTplItem> list = baosongTplItemService.list(queryWrapper);
		 return Result.OK(list);
	 }

	@AutoLog(value = "报送模板项-添加")
	@ApiOperation(value="报送模板项-添加", notes="报送模板项-添加")
	@RequiresPermissions("baosong:baosong_tpl_item:add")
	@PostMapping(value = "/add")
	public Result<BaosongTplItem> add(@RequestBody BaosongTplItem baosongTplItem) {
		baosongTplItemService.save(baosongTplItem);
		return Result.OK(baosongTplItem);
	}

	@AutoLog(value = "报送模板项-编辑")
	@ApiOperation(value="报送模板项-编辑", notes="报送模板项-编辑")
	@RequiresPermissions("baosong:baosong_tpl_item:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<BaosongTplItem> edit(@RequestBody BaosongTplItem baosongTplItem) {
		baosongTplItemService.updateById(baosongTplItem);
		return Result.OK(baosongTplItem);
	}

	@AutoLog(value = "报送模板项-通过id删除")
	@ApiOperation(value="报送模板项-通过id删除", notes="报送模板项-通过id删除")
	@RequiresPermissions("baosong:baosong_tpl_item:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		baosongTplItemService.removeById(id);
		return Result.OK("删除成功!");
	}

	@AutoLog(value = "报送模板项-批量删除")
	@ApiOperation(value="报送模板项-批量删除", notes="报送模板项-批量删除")
	@RequiresPermissions("baosong:baosong_tpl_item:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.baosongTplItemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	@ApiOperation(value="报送模板项-通过id查询", notes="报送模板项-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BaosongTplItem> queryById(@RequestParam(name="id",required=true) String id) {
		BaosongTplItem baosongTplItem = baosongTplItemService.getById(id);
		if(baosongTplItem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(baosongTplItem);
	}

    @RequiresPermissions("baosong:baosong_tpl_item:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BaosongTplItem baosongTplItem) {
        return super.exportXls(request, baosongTplItem, BaosongTplItem.class, "报送模板项");
    }

    @RequiresPermissions("baosong:baosong_tpl_item:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BaosongTplItem.class);
    }

	@PostMapping(value = "/saveBatch")
	public Result<String> saveBatch(@RequestBody BaosongTplItem tplItem) {
		if(tplItem.getItemids()!=null&& tplItem.getItemids().size()>0){
			QueryWrapper<BaosongTplItem> queryWrapper = new QueryWrapper<>();
			queryWrapper.in("id",tplItem.getItemids());
			baosongTplItemService.update(tplItem,queryWrapper);
		}
		return Result.OK("更新成功！");
	}
 }
