package org.stm.audit.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.audit.entity.AuditActRelOthers;
import org.stm.audit.entity.AuditInnerCtrl;
import org.stm.audit.entity.AuditInnerCtrlItem;
import org.stm.audit.service.*;
import org.stm.utils.CommonUtil;
import org.stm.utils.StmConstans;
import org.stm.utils.UserUtil;

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

/**
 * @Description: 内控制度
 * @Author: jeecg-boot
 * @Date:   2024-08-23
 * @Version: V1.0
 */
@Api(tags="内控制度")
@RestController
@RequestMapping("/audit/auditInnerCtrl")
@Slf4j
public class AuditInnerCtrlController extends JeecgController<AuditInnerCtrl, IAuditInnerCtrlService> {
	@Autowired
	private IAuditInnerCtrlService auditInnerCtrlService;
	@Autowired
	private IAuditLabelRelLibService stLabelRelLibService;
	@Autowired
	private IAuditActRelOthersService stActRelOthersService;
	@Autowired
	private IAuditInnerCtrlItemService auditInnerCtrlItemService;
	/**
	 * 分页列表查询
	 *
	 * @param auditInnerCtrl
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "内控制度-分页列表查询")
	@ApiOperation(value="内控制度-分页列表查询", notes="内控制度-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<AuditInnerCtrl>> queryPageList(AuditInnerCtrl auditInnerCtrl,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditInnerCtrl> queryWrapper = QueryGenerator.initQueryWrapper(auditInnerCtrl, req.getParameterMap());
		Page<AuditInnerCtrl> page = new Page<AuditInnerCtrl>(pageNo, pageSize);
		IPage<AuditInnerCtrl> pageList = auditInnerCtrlService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@GetMapping(value = "/listAll")
	public Result<List<AuditInnerCtrl>> listAll(AuditInnerCtrl auditInnerCtrl,
													   HttpServletRequest req) {
		QueryWrapper<AuditInnerCtrl> queryWrapper = QueryGenerator.initQueryWrapper(auditInnerCtrl, req.getParameterMap());
		List<AuditInnerCtrl> list = auditInnerCtrlService.list(queryWrapper);
		return Result.OK(list);
	}
	
	/**
	 *   添加
	 *
	 * @param auditInnerCtrl
	 * @return
	 */
	@AutoLog(value = "内控制度-添加")
	@ApiOperation(value="内控制度-添加", notes="内控制度-添加")
	@PostMapping(value = "/add")
	public Result<AuditInnerCtrl> add(@RequestBody AuditInnerCtrl auditInnerCtrl) {
		auditInnerCtrl.setCreateUser(UserUtil.getUserCode());
		auditInnerCtrlService.save(auditInnerCtrl);
		Integer tid =  auditInnerCtrl.getId();
		return Result.OK(auditInnerCtrl);
	}
	
	/**
	 *  编辑
	 * @param auditInnerCtrl
	 * @return
	 */
	@AutoLog(value = "内控制度-编辑")
	@ApiOperation(value="内控制度-编辑", notes="内控制度-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<AuditInnerCtrl> edit(@RequestBody AuditInnerCtrl auditInnerCtrl) {
		auditInnerCtrlService.updateById(auditInnerCtrl);
		return Result.OK(auditInnerCtrl);
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "内控制度-通过id删除")
	@ApiOperation(value="内控制度-通过id删除", notes="内控制度-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		auditInnerCtrlService.removeById(id);
		QueryWrapper<AuditInnerCtrlItem> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("checkid",id);
		auditInnerCtrlItemService.remove(queryWrapper);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "内控制度-批量删除")
	@ApiOperation(value="内控制度-批量删除", notes="内控制度-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		List<String> idList = Arrays.asList(ids.split(","));
		this.auditInnerCtrlService.removeByIds(idList);
		QueryWrapper<AuditInnerCtrlItem> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("checkid",idList);
		auditInnerCtrlItemService.remove(queryWrapper);
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "内控制度-通过id查询")
	@ApiOperation(value="内控制度-通过id查询", notes="内控制度-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<AuditInnerCtrl> queryById(@RequestParam(name="id",required=true) String id) {
		AuditInnerCtrl auditInnerCtrl = auditInnerCtrlService.getById(id);
		if(auditInnerCtrl==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditInnerCtrl);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param auditInnerCtrl
    */
    @RequiresPermissions("audit:audit_inner_ctrl:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AuditInnerCtrl auditInnerCtrl) {
        return super.exportXls(request, auditInnerCtrl, AuditInnerCtrl.class, "内控制度");
    }

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


	@GetMapping(value = "/queryRuleItems")
	public Result<List<Map<String,Object>>> queryRuleItems(AuditInnerCtrl auditInnerCtrl,
												HttpServletRequest req) {
		String strTp = req.getParameter("srhTp");
		List<Map<String,Object>> list = null;
		if(strTp!=null&&strTp.equals("2")) {
			list = auditInnerCtrlService.queryRuleItems();
		} else {
			list = auditInnerCtrlService.queryCtrlOrderRuleItems();
		}
		return Result.OK(list);
	}
}
