package org.jeecg.modules.stm.base.lable.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.stm.audit.entity.AuditLabelRelLib;
import org.jeecg.modules.stm.audit.service.IAuditLabelRelLibService;
import org.jeecg.modules.stm.base.lable.entity.StLable;
import org.jeecg.modules.stm.base.lable.service.IStLableService;

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.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
 
 
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: st_lable
 * @Author: yzh
 * @Date:   2023-03-04
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/base.lable/stLable")
@Slf4j
public class StLableController extends JeecgController<StLable, IStLableService> {
	@Autowired
	private IStLableService stLableService;
	 @Autowired
	 private IAuditLabelRelLibService stLabelRelLibService;
	
	/**
	 * 分页列表查询
	 *
	 * @param stLable
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "标签管理-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<StLable>> queryPageList(StLable stLable,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<StLable> queryWrapper = QueryGenerator.initQueryWrapper(stLable, req.getParameterMap());
		Page<StLable> page = new Page<StLable>(pageNo, pageSize);
		IPage<StLable> pageList = stLableService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/queryAllData")
	 public Result<List<StLable>> queryAlllist(StLable stLable,HttpServletRequest req) {
		 QueryWrapper<StLable> queryWrapper = QueryGenerator.initQueryWrapper(stLable, req.getParameterMap());
		 List<StLable> dataList = stLableService.list(queryWrapper);
		 return Result.OK(dataList);
	 }
	
	/**
	 *   添加
	 *
	 * @param stLable
	 * @return
	 */
	@AutoLog(value = "标签管理-添加")
	  
	@RequiresPermissions("base.lable:st_lable:add")
	@PostMapping(value = "/add")
	public Result<StLable> add(@RequestBody StLable stLable) {
		stLableService.save(stLable);
		return Result.OK(stLable);
	}
	
	/**
	 *  编辑
	 *
	 * @param stLable
	 * @return
	 */
	@AutoLog(value = "标签管理-编辑")
	  
	@RequiresPermissions("base.lable:st_lable:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<StLable> edit(@RequestBody StLable stLable) {
		stLableService.updateById(stLable);
		return Result.OK(stLable);
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "标签管理-通过id删除")
	  
	@RequiresPermissions("base.lable:st_lable:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		StLable stLable =  this.stLableService.getById(id);
		QueryWrapper<AuditLabelRelLib> queryWrapperRel = new QueryWrapper<>();
		queryWrapperRel.eq("label_code", stLable.getLableCode());
		this.stLabelRelLibService.remove(queryWrapperRel);
		stLableService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "标签管理-批量删除")
	  
	@RequiresPermissions("base.lable:st_lable:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		QueryWrapper<StLable> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("id",ids.split(","));
		List<StLable> list =  this.stLableService.list(queryWrapper);
		List<String> codes  = new ArrayList<>();
		if(list.size()>0) {
			for (StLable stLable : list) {
				codes.add(stLable.getLableCode());
			}
			QueryWrapper<AuditLabelRelLib> queryWrapperRel = new QueryWrapper<>();
			queryWrapper.in("label_code", codes);
			this.stLabelRelLibService.remove(queryWrapperRel);
		}

		this.stLableService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "标签管理-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<StLable> queryById(@RequestParam(name="id",required=true) String id) {
		StLable stLable = stLableService.getById(id);
		if(stLable==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(stLable);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param stLable
    */
    @RequiresPermissions("base.lable:st_lable:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StLable stLable) {
        return super.exportXls(request, stLable, StLable.class, "st_lable");
    }

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

}
