package org.stm.act.metric.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.stm.act.metric.entity.StMetric;
import org.stm.act.metric.service.IStMetricService;
import org.stm.act.metric.vo.StCaMainMetricVo;
import org.stm.act.metric.vo.StMetricVo;

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

/**
 * @Description: st_metric
 * @Author: yzh
 * @Date:   2023-03-02
 * @Version: V1.0
 */
@Api(tags="度量管理")
@RestController
@RequestMapping("/act.metric/stMetric")
@Slf4j
public class StMetricController extends JeecgController<StMetric, IStMetricService> {
	@Autowired
	private IStMetricService stMetricService;
	
	/**
	 * 分页列表查询
	 *
	 * @param stMetric
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "度量管理-分页列表查询")
	@ApiOperation(value="度量管理-分页列表查询", notes="度量管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<StMetric>> queryPageList(StMetric stMetric,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
//		QueryWrapper<StMetric> queryWrapper = QueryGenerator.initQueryWrapper(stMetric, req.getParameterMap());
//		Page<StMetric> page = new Page<StMetric>(pageNo, pageSize);
//		IPage<StMetric> pageList = stMetricService.page(page, queryWrapper);
		IPage<StMetric> pageList = stMetricService.defPage(stMetric,pageNo,pageSize,req.getParameterMap());
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param stMetric
	 * @return
	 */
	@AutoLog(value = "度量管理-添加")
	@ApiOperation(value="度量管理-添加", notes="度量管理-添加")
	@RequiresPermissions("act.metric:st_metric:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody StMetricVo stMetric) {
		stMetricService.save(stMetric);
		return Result.OK("添加成功！");
	}

	/**
	 *   添加
	 *
	 * @param stMetric
	 * @return
	 */
	@AutoLog(value = "度量管理-关联")
	@ApiOperation(value="度量管理-关联", notes="度量管理-关联")
	@RequiresPermissions("act.metric:st_metric:add")
	@PostMapping(value = "/addRef")
	public Result<String> addRef(@RequestBody StCaMainMetricVo stCaMainMetricVo) {
		stMetricService.saveRef(stCaMainMetricVo);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param stMetric
	 * @return
	 */
	@AutoLog(value = "度量管理-编辑")
	@ApiOperation(value="度量管理-编辑", notes="度量管理-编辑")
	@RequiresPermissions("act.metric:st_metric:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody StMetric stMetric) {
		stMetricService.updateById(stMetric);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "度量管理-通过id删除")
	@ApiOperation(value="度量管理-通过id删除", notes="度量管理-通过id删除")
	@RequiresPermissions("act.metric:st_metric:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		stMetricService.removeById(id);
		return Result.OK("删除成功!");
	}

	@AutoLog(value = "度量管理-通过id删除")
	@ApiOperation(value="度量管理-通过id删除", notes="度量管理-通过id删除")
	@RequiresPermissions("act.metric:st_metric:delete")
	@DeleteMapping(value = "/deleteRef")
	public Result<String> deleteRef(@RequestParam(name="id",required=true) String id,@RequestParam(name = "caNo",required=true) String caNo) {
		stMetricService.removeRefById(id,caNo);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "度量管理-批量删除")
	@ApiOperation(value="度量管理-批量删除", notes="度量管理-批量删除")
	@RequiresPermissions("act.metric:st_metric:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stMetricService.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<StMetric> queryById(@RequestParam(name="id",required=true) String id) {
		StMetric stMetric = stMetricService.getById(id);
		if(stMetric==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(stMetric);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param stMetric
    */
    @RequiresPermissions("act.metric:st_metric:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StMetric stMetric) {
        return super.exportXls(request, stMetric, StMetric.class, "st_metric");
    }

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

}
