package com.org.hktshop.service.Admin;

import com.org.hktshop.Interface.admin.IHktAdminResourceService;
import com.org.hktshop.Interface.admin.IHktAdminRoleService;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.base.MongoHelper;
import com.org.hktshop.entity.admin.HktAdminResource;
import com.org.hktshop.entity.admin.HktAdminRole;
import com.org.hktshop.utils.DateUtil;
import com.org.hktshop.utils.Ret;
import com.org.hktshop.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/***
 * 管理端资源文件处理
 * @author tw
 */
@Service
public class HktAdminResourceServiceImpl extends BaseService<HktAdminResource> implements IHktAdminResourceService {

    @Autowired
    IHktAdminRoleService roleService;

    @Autowired
    MongoHelper<HktAdminResource> mongoHelper;

    /***
     *
     * @param isDelete 是否全部清除后从数据库中再次load 数据
     * @return
     */
    @Override
    public List<HktAdminResource> queryList(boolean isDelete) {

        List<HktAdminResource> resourceList = new ArrayList<>();
        if (isDelete) mongoHelper.delete(new Query(), HktAdminResource.class);
        else resourceList = mongoHelper.find(new HktAdminResource(), HktAdminResource.class);
        if (resourceList.isEmpty()) {
            resourceList = this.queryList(new HktAdminResource());
            /*for (HktAdminResource resource : resourceList) {
                mongoHelper.save(resource);
            }*/
                mongoHelper.save(resourceList);
        }
        return resourceList;
    }

    /***
     * 查询单个资源文件，只能在缓存上查询
     * @param resource
     * @return
     */
    @Override
    public HktAdminResource queryResource(HktAdminResource resource) {
        HktAdminResource find = mongoHelper.findOne(resource, HktAdminResource.class);
        return find;
    }

    /****
     * 新增/修改资源文件
     * @param resource
     * @return
     */
    @Override
    public Ret updateResource(HktAdminResource resource) {
        Ret ret = new Ret();
        if (StringUtils.isNull(resource.getId())) {
            /***
             * 新增
             * 1.新增资源文件
             * 2.系统管理员新增资源权限
             */
            HktAdminResource find = mongoHelper.findOne(resource, HktAdminResource.class);
            if (find != null) return Ret.create().setFail().set("msg", "新增内容已存在,不可再次新增");
            resource.setCreateTime(DateUtil.getDateTime());

            if (this.updateJdbc(resource)) {//存入数据库
                ret.setOk().set("msg", "新增资源成功");
            } else {
                ret.setFail().set("msg", "新增资源失败");
            }
            String sql = String.format("select id,parent_id as parentId,name,resource_sn as resourceSn,sort,modify_time as modifyTime,create_time as createTime from hkt_admin_resource where parent_id=%s and name=\"%s\" and resource_sn=\"%s\"", resource.getParentId(), resource.getName(), resource.getResourceSn());
            HktAdminResource query = this.baseJdbcDao.query(sql, resource);
            mongoHelper.save(query);//存入mongoDB
        } else {
            //更新
            HktAdminResource find = new HktAdminResource();
            find.setId(resource.getId());
            find = mongoHelper.findOne(find, HktAdminResource.class);
            if (find == null) return Ret.create().setFail().set("msg", "不存在需要修改的对象");

            if (!StringUtils.isNull(resource.getName())) find.setName(resource.getName());
            if (!StringUtils.isNull(resource.getParentId())) find.setParentId(resource.getParentId());
            if (!StringUtils.isNull(resource.getResourceSn())) find.setResourceSn(resource.getResourceSn());
            if (!StringUtils.isNull(resource.getSort())) find.setSort(resource.getSort());
            find.setModifyTime(DateUtil.getDateTime());

            if (this.updateJdbc(find)) {
                //MongoDB更新
                mongoHelper.mongoUpdate(resource.getId(), find, HktAdminResource.class);
                ret.setOk().set("msg", "修改资源信息成功");
            } else {
                ret.setFail().set("msg", "修改资源信息失败");
            }

        }
        return ret;
    }

    /***
     * 删除资源文件
     * 1.删除资源
     * 2.删除对应角色下的资源授权
     * @param resource
     * @return
     */
    @Override
    public Ret deleteResource(HktAdminResource resource) {
        Ret ret = new Ret();
        if (this.deleteById(resource.getId(), resource)) {
            mongoHelper.removeById(resource.getId(), HktAdminResource.class);
            ret.setOk().set("msg", "删除资源成功");
        } else {
            ret.setFail().set("msg", "修改资源失败");
        }
        return ret;
    }

    //查询角色资源
    @Override
    public List<HktAdminResource> queryRoleResource(HktAdminRole role) {
        List<HktAdminResource> res = new ArrayList<>();
        String ids = role.getResourceIds();
        if (StringUtils.isNull(ids)) return null;

        if (ids.contains("-1")) {

            List<HktAdminResource> hktAdminResource = mongoHelper.find(new HktAdminResource(), HktAdminResource.class);
            return  hktAdminResource;
        } else {
            HktAdminResource resource = new HktAdminResource();
            String[] idsArr = ids.split(",");
            //先从mongonDB查询
            for (String id : idsArr) {
                resource.setId(id);
                HktAdminResource hktAdminResource = mongoHelper.findOne(resource, HktAdminResource.class);
                if (hktAdminResource != null) {
                    res.add(hktAdminResource);
                }
            }
         /*   if (!CollectionUtils.isEmpty(res)) {
                return res;
            }*/
            //mongodb无数据从数据库查
            String sql = String.format("select id,parent_id as parentId,name,resource_sn as resourceSn,sort,modify_time as modifyTime,create_time as createTime from hkt_admin_resource where id in(%s)", ids);
            res = this.baseJdbcDao.queryList(sql, new HktAdminResource());
            //把从数据库查询出来的数据存入MongoDB
//            saveDatatoMongoDB(res);
        }
        return res;
    }

    /**
     * 数据存入MongoDB
     *
     * @param res
     */
    private void saveDatatoMongoDB(List<HktAdminResource> res) {
        /*for (HktAdminResource resource : res) {
            mongoHelper.save(resource);
        }*/
        mongoHelper.save(res);
    }
}
