package com.winsun.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.Regular;
import com.winsun.mapper.RegularMapper;
import com.winsun.utils.MyBatisPlusUpdateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 正则校检
 * @Author: xiangli
 * @Date:
 */
@Slf4j
@RestController
@RequestMapping("/regular")
public class RegularController extends BaseController {
    @Autowired
    private RegularMapper regularMapper;
    /**
     * 正则添加
     *
     * @param regular 添加对象json字符串
     * @return
     */
    @Permission(menuname = "添加正则信息", value = "insert", method = RequestMethod.POST)
    public ResponseData<String> insertProduct(@RequestParam("regular") String regular) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Regular pro = JSON.parseObject(regular, Regular.class);
        regularMapper.insert(pro);
        return ResponseData.success("添加成功！");
    }

    /**
     * 正则删除
     *
     * @param regularId 需要删除的id
     * @return
     */
    @Permission(menuname = "删除正则信息", value = "delete", method = RequestMethod.POST)
    public ResponseData<String> deleteProduct(@RequestParam(name="regularId", required = false) String regularId,@RequestParam(name="isShow", required = false) String isShow) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("regular_id", regularId);
        Map<String, Object> dataMapping = new HashMap<>();
        dataMapping.put("is_show", isShow);
        regularMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
        return ResponseData.success("操作成功！");
    }

    /**
     * 正则信息编辑
     *
     * @param regular 编辑对象json字符串
     * @return
     */
    @Permission(menuname = "修改正则信息", value = "update", method = RequestMethod.POST)
    public ResponseData<String> updateProduct(@RequestParam("regular") String regular) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Regular pro = JSON.parseObject(regular, Regular.class);
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("regular_id", pro.getRegularId());
        Map<String, Object> dataMapping = new HashMap<>();
        dataMapping.put("regular_name", pro.getRegularName());
        dataMapping.put("regular", pro.getRegular());
        dataMapping.put("tips", pro.getTips());
        dataMapping.put("placeholder", pro.getPlaceholder());
        dataMapping.put("is_show", pro.getIsShow());
        regularMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
        return ResponseData.success("修改成功！");
    }

    /**
     * 正则查詢展示
     *
     * @param regularName 正则名称
     * @param pageSize
     * @return
     */
    @Permission(menuname = "查询正则信息", value = "list", method = RequestMethod.POST)
    public ResponseData<Page<Regular>> listProduct(@RequestParam(name = "regularName", required = false) String regularName,
                                                   @RequestParam(name = "pageNo") int pageNo, @RequestParam(name = "pageSize") int pageSize) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Wrapper<Regular> wrapper = new EntityWrapper();
        wrapper.like(StringUtils.isNotBlank(regularName), "regular_name", regularName, SqlLike.DEFAULT);
        Page<Regular> page = new Page<>(pageNo, pageSize);
        List<Regular> regulars = regularMapper.selectPage(page, wrapper);
        page.setRecords(regulars);
        return ResponseData.success(page, "查询成功！");
    }

}
