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 lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
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.AuditLabelRelLib;
import org.stm.audit.service.IAuditLabelRelLibService;

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

/**
 * @Description: 标准库与标签关系表
 * @Author: jeecg-boot
 * @Date:   2024-08-01
 * @Version: V1.0
 */
@Api(tags="标准库与标签关系表")
@RestController
@RequestMapping("/audit/stLabelRelLib")
@Slf4j
public class AuditLabelRelLibController extends JeecgController<AuditLabelRelLib, IAuditLabelRelLibService> {
	@Autowired
	private IAuditLabelRelLibService stLabelRelLibService;

	@GetMapping(value = "/list")
	public Result<IPage<AuditLabelRelLib>> queryPageList(AuditLabelRelLib auditLabelRelLib,
														 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														 HttpServletRequest req) {
		QueryWrapper<AuditLabelRelLib> queryWrapper = QueryGenerator.initQueryWrapper(auditLabelRelLib, req.getParameterMap());
		Page<AuditLabelRelLib> page = new Page<AuditLabelRelLib>(pageNo, pageSize);
		IPage<AuditLabelRelLib> pageList = stLabelRelLibService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AuditLabelRelLib auditLabelRelLib) {
		stLabelRelLibService.save(auditLabelRelLib);
		return Result.OK("添加成功！");
	}

    @GetMapping(value = "/batchAdd")
    public Result<String> saveBatchActRelations(@RequestParam Integer tp,
                    @RequestParam String tids,
                    @RequestParam String labelCodes,
                    @RequestParam String labelNames, HttpServletRequest req) {
        String[] labelCodeAry = labelCodes.split(",");
        String[] labelNameAry = labelNames.split(",");
		String[] tidAry = tids.split(",");
		deleteBatchByQueryWrapper(tp,tidAry);
		for(int i=0;i<labelCodeAry.length;i++) {
            List<AuditLabelRelLib> saveList = new ArrayList<>();
			for (String tid : tidAry) {
				AuditLabelRelLib labelRel = new AuditLabelRelLib();
				labelRel.setTp(tp);
				labelRel.setLabelCode(labelCodeAry[i]);
				labelRel.setLabelName(labelNameAry[i]);
				labelRel.setTid(Integer.parseInt(tid));
				saveList.add(labelRel);
			}
            stLabelRelLibService.saveBatch(saveList);
        }
        return Result.OK("保存成功");
    }

	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AuditLabelRelLib auditLabelRelLib) {
		stLabelRelLibService.updateById(auditLabelRelLib);
		return Result.OK("编辑成功!");
	}

	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		stLabelRelLibService.removeById(id);
		return Result.OK("删除成功!");
	}

	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stLabelRelLibService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	@DeleteMapping(value = "/deleteByLabelCodes")
	public Result<String> deleteByLabelCodes(@RequestParam(name="labelCodes",required=true) String labelCodes) {
		QueryWrapper<AuditLabelRelLib> queryWrapper =new QueryWrapper<>();
		queryWrapper.in("label_code",labelCodes.split(","));
		this.stLabelRelLibService.remove(queryWrapper);
		return Result.OK("批量删除成功!");
	}

	@GetMapping(value = "/queryById")
	public Result<AuditLabelRelLib> queryById(@RequestParam(name="id",required=true) String id) {
		AuditLabelRelLib auditLabelRelLib = stLabelRelLibService.getById(id);
		if(auditLabelRelLib ==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditLabelRelLib);
	}


    @RequiresPermissions("audit:st_label_rel_lib:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AuditLabelRelLib auditLabelRelLib) {
        return super.exportXls(request, auditLabelRelLib, AuditLabelRelLib.class, "标准库与标签关系表");
    }

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

	private void deleteBatchByQueryWrapper(Integer tp,String[] tidAry) {
		QueryWrapper<AuditLabelRelLib> queryWrapper =new QueryWrapper<>();
		queryWrapper.eq("tp",tp).in("tid",tidAry);
		this.stLabelRelLibService.remove(queryWrapper);
	}


}
