package com.org.hktshop.service.System;

import com.org.hktshop.Interface.system.IHktNewsArticleService;
import com.org.hktshop.Interface.system.IHktNewsCategoryService;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.base.MongoHelper;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.system.HktNewsArticle;
import com.org.hktshop.entity.system.HktNewsCategory;
import com.org.hktshop.utils.DateUtil;
import com.org.hktshop.utils.ExecSqlLib;
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 java.util.List;

@Service
public class HktNewsCategoryImpl extends BaseService<HktNewsCategory> implements IHktNewsCategoryService {

    @Autowired
    MongoHelper<HktNewsCategory> mongoHelper;

    @Autowired
    IHktNewsArticleService articleService;

    @Override
    public void InitData() {
        List<HktNewsCategory> categories = mongoHelper.find(new HktNewsCategory(), HktNewsCategory.class);
        if (categories.isEmpty()) {
            categories = queryList(new HktNewsCategory());
            for (HktNewsCategory newsCategoryItem : categories) {
                mongoHelper.save(newsCategoryItem);
            }
        }
    }

    @Override
    public Page<HktNewsCategory> queryHktNewsCategoryPages(HktNewsCategory hktNewsCategory) {
        Page<HktNewsCategory> page = new Page<HktNewsCategory>();
        if (hktNewsCategory.getPageSize() != 0 && hktNewsCategory.getPageNum() != 0) {
            page.setMsg("页数不能为零");
        }
        page = mongoHelper.findPage(hktNewsCategory, HktNewsCategory.class);
        return page;
    }

    @Override
    public Ret updateHktNewsCategory(HktNewsCategory hktNewsCategory) {

        if (StringUtils.isNull(hktNewsCategory.getId())) {
            HktNewsCategory find = new HktNewsCategory();
            find.setName(hktNewsCategory.getName());
            find.setLanguageId(hktNewsCategory.getLanguageId());
            HktNewsCategory category = mongoHelper.findOne(find, HktNewsCategory.class);
            if (category != null) return Ret.create().setFail().setMsg("不可重复添加");

            if (this.updateJdbc(hktNewsCategory)) {//存入数据库

                HktNewsCategory query = this.baseJdbcDao.query(ExecSqlLib.GetQuerySql(find), hktNewsCategory);
                mongoHelper.save(query);//存入mongoDB
                return Ret.create().setOk().setMsg("新增文章成功");
            } else {
                return Ret.create().setOk().setMsg("新增文章失败");
            }
        } else {
            //更新
            HktNewsCategory find = new HktNewsCategory();
            find.setId(hktNewsCategory.getId());
            find = mongoHelper.findOne(find, HktNewsCategory.class);
            if (find == null) return Ret.create().setFail().set("msg", "不存在需要修改的对象");

            if (!StringUtils.isNull(hktNewsCategory.getName())) find.setName(hktNewsCategory.getName());
            if (!StringUtils.isNull(hktNewsCategory.getSort())) find.setSort(hktNewsCategory.getSort());
            find.setUpdateTime(DateUtil.getDateTime());

            if (this.updateJdbc(find)) {
                //MongoDB更新
                mongoHelper.mongoUpdate(hktNewsCategory.getId(), find, HktNewsCategory.class);
                return Ret.create().setOk().set("msg", "修改文章成功");
            } else {
                return Ret.create().setFail().set("msg", "修改文章失败");
            }
        }
    }

    @Override
    public Ret deleteHktNewsCategory(HktNewsCategory hktNewsCategory) {
        Ret ret = Ret.create();

        if (StringUtils.isNull(hktNewsCategory.getNewsNum())) {
            HktNewsArticle article = new HktNewsArticle();
            article.setCid(hktNewsCategory.getId());
            article.setPageNum(1);
            article.setPageSize(10);
            Page<HktNewsArticle> article1 = articleService.queryPage(article);
            if (article1.getTotal() > 0) {
                return ret.setFail().setMsg("分类已经被引用,不可删除");
            }

        } else {
            if (hktNewsCategory.getNewsNum() > 0) {//文章数量为零时才能删除
                return ret.setFail().setMsg("分类已经被引用,不可删除");
            }
        }
        if (this.deleteById(hktNewsCategory.getId(), hktNewsCategory)) {
            mongoHelper.removeById(hktNewsCategory.getId(), HktNewsCategory.class);//MongoDB删除对应信息
            return Ret.create().setOk();
        }
        return ret;
    }

    @Override
    public List<HktNewsCategory> queryNewsCategoryList(HktNewsCategory hktNewsCategory) {
        return mongoHelper.find(hktNewsCategory, HktNewsCategory.class);
    }

    @Override
    public HktNewsCategory queryCategory(HktNewsCategory category) {
        return mongoHelper.findOne(category, HktNewsCategory.class);
    }
}
