package com.ag.dynamicmodule.wbgrid.domain;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ag.base.DefaultBussniessImpl;
import com.ag.entity.webbuiler.WbDeptPerm;
import com.ag.util.UUIDUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Created by yangxh on 2022/6/20.
 */
@Service
public class WbDataPermService {

    @Autowired
    private DefaultBussniessImpl bizDao;

    public void save(org.json.JSONObject object, HttpServletResponse response, HttpServletRequest request){
        String data = object.getString("data");
        System.out.println(data);
        List<WbDeptPerm> perms = JSON.parseArray(data, WbDeptPerm.class);
        if (CollectionUtil.isEmpty(perms)){
            return;
        }
        WbDeptPerm query = new WbDeptPerm();
        query.setDeptId(perms.get(0).getDeptId());
        List<WbDeptPerm> finds = bizDao.findByEntity(query, WbDeptPerm.class);
        perms.removeIf(x->isEqual(x, finds));
        perms.forEach(x->x.setId(UUIDUtil.getUUID()));
        bizDao.saveAll(perms);
    }

    private boolean isEqual(WbDeptPerm perm, List<WbDeptPerm> finds){
        for (WbDeptPerm find : finds){
            if (ObjectUtil.equal(perm.getDataDeptId(), find.getDataDeptId())){
                return true;
            }
        }
        return false;
    }

}
