package org.jeecg.modules.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 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.jeecg.modules.stm.audit.service.IAuditLabelRelLibService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.jeecg.modules.stm.audit.entity.AuditAct;
import org.jeecg.modules.stm.audit.entity.AuditActRelOthers;
import org.jeecg.modules.stm.audit.service.IAuditActRelOthersService;
import org.jeecg.modules.stm.audit.service.IAuditActService;
import org.jeecg.modules.stm.base.domain.service.IStDomainService;
import org.jeecg.modules.stm.utils.StmConstans;

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


/**
 * @Description: 控制项表
 * @Author: jeecg-boot
 * @Date:   2024-08-31
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/audit/auditAct")
@Slf4j
public class AuditActController extends JeecgController<AuditAct, IAuditActService> {
	@Autowired
	private IAuditActService auditActService;
	@Autowired
	private IStDomainService stDomainService;
	@Autowired
	private IAuditLabelRelLibService stLabelRelLibService;
	@Autowired
	private IAuditActRelOthersService stActRelOthersService;

	
	/**
	 * 分页列表查询
	 *
	 * @param auditAct
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "控制项表-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<AuditAct>> queryPageList(AuditAct auditAct,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditAct> queryWrapper = QueryGenerator.initQueryWrapper(auditAct, req.getParameterMap());
		Page<AuditAct> page = new Page<AuditAct>(pageNo, pageSize);

		IPage<AuditAct> pageList = auditActService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@GetMapping(value = "/queryByIds")
	public Result<List<AuditAct>> queryByIdList(String ids, HttpServletRequest req) {
		QueryWrapper<AuditAct> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("id",ids.split(","));
		List<AuditAct> retList = auditActService.list( queryWrapper);
		return Result.OK(retList);
	}

	@GetMapping(value = "/queryActByIds")
	public Result<List<AuditAct>> queryActByIds(String ids, HttpServletRequest req) {
		QueryWrapper<AuditAct> queryWrapper = new QueryWrapper<>();
		if(ids==null){
			return Result.OK(new ArrayList<>());
		}
		queryWrapper.in("id",ids.split(","));
		List<AuditAct> list = auditActService.list(queryWrapper);
		return Result.OK(list);
	}



	/**
	 *   添加
	 *
	 * @param auditAct
	 * @return
	 */
	@AutoLog(value = "控制项表-添加")
	  
	@PostMapping(value = "/add")
	public Result<AuditAct> add(@RequestBody AuditAct auditAct) {
		if(checkExist(auditAct)) {
			return Result.error("控制项目名称已经存在，保存失败");
		}
		auditActService.save(auditAct);
		String labelCodes = auditAct.getLabels();
		Integer tid =  auditAct.getId();
		if(labelCodes!=null) {
			stLabelRelLibService.saveLabelRel(labelCodes,tid,StmConstans.Audit_TP_ACT);
		}
		return Result.OK(auditAct);
	}
	
	/**
	 *  编辑
	 *
	 * @param auditAct
	 * @return
	 */
	@AutoLog(value = "控制项表-编辑")
	  
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AuditAct auditAct) {
		if(checkExist(auditAct)) {
			return Result.error("控制项目名称已经存在，保存失败");
		}
		auditActService.updateById(auditAct);
		String labelCodes = auditAct.getLabels();
		Integer tid =  auditAct.getId();
		stLabelRelLibService.deleteLabelRel(tid,StmConstans.Audit_TP_ACT);
		if(labelCodes!=null&&!labelCodes.equals("")){
			stLabelRelLibService.saveLabelRel(labelCodes,tid,StmConstans.Audit_TP_ACT);
		}
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "控制项表-通过id删除")
	  
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		QueryWrapper<AuditActRelOthers> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("act_id",id);
		stActRelOthersService.remove(queryWrapper);
		auditActService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "控制项表-批量删除")
	  
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		QueryWrapper<AuditActRelOthers> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("act_id",ids.split(","));
		stActRelOthersService.remove(queryWrapper);
		this.auditActService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "控制项表-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<AuditAct> queryById(@RequestParam(name="id",required=true) String id) {
		AuditAct auditAct = auditActService.getById(id);
		if(auditAct==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditAct);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param auditAct
    */
    @RequiresPermissions("audit:audit_act:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AuditAct auditAct) {
        return super.exportXls(request, auditAct, AuditAct.class, "控制项表");
    }

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


	 @GetMapping(value = "/allActs")
	 public Result<List<AuditAct>> getAllActs(AuditAct auditAct,HttpServletRequest req) {
		 List<AuditAct> actList = auditActService.queryAllAct();
		 return Result.OK(actList);
	 }

	@GetMapping(value = "/mergeAct")
	public Result<String> mergeAct(@RequestParam String ids,@RequestParam Integer tId, HttpServletRequest req) {
		String[] idAry = ids.split(",");
		QueryWrapper<AuditAct> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("id",idAry);
		auditActService.remove(queryWrapper);

		AuditActRelOthers auditActRelOthers = new AuditActRelOthers();
		auditActRelOthers.setActId(tId);
		QueryWrapper<AuditActRelOthers> queryRelWrapper = new QueryWrapper<>();
		queryRelWrapper.in("act_id",idAry);
		stActRelOthersService.update(auditActRelOthers,queryRelWrapper);

		return Result.OK("合并成功");
	}

	private boolean checkExist(AuditAct act) {
		QueryWrapper<AuditAct> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("ca_name",act.getCaName());
		if(act.getId()!=null){
			queryWrapper.ne("id",act.getId());
		}
		List list = auditActService.list(queryWrapper);
		if(list==null||list.size()==0){
			return false;
		}

		return true;
	}

}
