package com.org.hktshop.service.System;

import com.org.hktshop.Interface.system.IHktSysHelpsCategoryService;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.base.MongoHelper;
import com.org.hktshop.entity.admin.HktAdmin;
import com.org.hktshop.entity.system.HktSysHelpes;
import com.org.hktshop.entity.system.HktSysHelpsCategory;
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.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/****
 * 帮助分类
 *
 */
@Service
public class HktHelperCategoryServiceImpl extends BaseService<HktSysHelpsCategory> implements IHktSysHelpsCategoryService {

    @Autowired
    private MongoHelper<HktSysHelpsCategory> mongoHelper;

    /****
     * 查询帮助列表 商城上会使用
     * @param hktSysHelpsCategory
     * @return
     */
    @Override
    public List<HktSysHelpsCategory> queryLists(HktSysHelpsCategory hktSysHelpsCategory) {

        Query query = mongoHelper.createQuery(hktSysHelpsCategory, HktSysHelpsCategory.class);
        query.with(new Sort(Sort.Direction.ASC, "languageId", "sort"));

        List<HktSysHelpsCategory> list = mongoHelper.find(query, HktSysHelpsCategory.class);
        if (list.isEmpty()) {
            try {
                HktSysHelpsCategory hktSysHelpsCategory1 = new HktSysHelpsCategory();
                hktSysHelpsCategory1.setLanguageId(hktSysHelpsCategory.getLanguageId());
                list = queryList(hktSysHelpsCategory1);
                for (HktSysHelpsCategory category : list) {
                    mongoHelper.save(category);
                }
                list = mongoHelper.find(hktSysHelpsCategory, HktSysHelpsCategory.class);
            } catch (Exception ex) {
                //异常怎么处理
            }
        }
        return list;
    }

    @Override
    public HktSysHelpsCategory queryCategory(HktSysHelpsCategory hktSysHelpsCategory) {
        return mongoHelper.findOne(hktSysHelpsCategory, HktSysHelpsCategory.class);
    }


    /****
     * 新增/修改
     * @param admin
     * @param hktSysHelpsCategory
     * @return
     */
    @Override
    public Ret updateHktSysHelpsCategory(HktAdmin admin, HktSysHelpsCategory hktSysHelpsCategory) {
        Ret ret = Ret.create();

        if (StringUtils.isNull(hktSysHelpsCategory.getId())) {
            //新增
            //校验数据正确性
            hktSysHelpsCategory.setCreateTime(DateUtil.getDateTime());
            if (this.updateJdbc(hktSysHelpsCategory)) {

                List<HktSysHelpsCategory> list = queryList(hktSysHelpsCategory);
                for (HktSysHelpsCategory category : list) {
                    mongoHelper.save(category);
                }
                ret.setOk();
            } else {
                ret.setFail();
            }
        } else {
            //修改
            HktSysHelpsCategory helpes = new HktSysHelpsCategory();
            helpes.setId(hktSysHelpsCategory.getId());
            HktSysHelpsCategory find = mongoHelper.findOne(helpes, HktSysHelpsCategory.class);
            if (find == null) return ret.setFail().set("msg", "当前对象不存在");

            if (!StringUtils.isNull(hktSysHelpsCategory.getLanguageId()))
                find.setLanguageId(hktSysHelpsCategory.getLanguageId());
            if (!StringUtils.isNull(hktSysHelpsCategory.getName())) find.setName(hktSysHelpsCategory.getName());
            if (!StringUtils.isNull(hktSysHelpsCategory.getSort())) find.setSort(hktSysHelpsCategory.getSort());
            if (!StringUtils.isNull(hktSysHelpsCategory.getImg())) find.setImg(hktSysHelpsCategory.getImg());
            find.setModifyTime(DateUtil.getDateTime());

            if (this.updateJdbc(find)) {
                //成功更新
                mongoHelper.mongoUpdate(find.getId(), find, HktSysHelpsCategory.class);
                ret.setOk();
            } else {
                ret.setFail();
            }
        }
        return ret;
    }

    /***
     * 删除
     * @param admin
     * @param hktSysHelpsCategory
     * @return
     */
    @Override
    public Ret deleteHktSysHelpsCategory(HktAdmin admin, HktSysHelpsCategory hktSysHelpsCategory) {

        HktSysHelpsCategory category = new HktSysHelpsCategory();
        category.setId(hktSysHelpsCategory.getId());
        category = mongoHelper.findOne(category, HktSysHelpsCategory.class);
        if (category == null) return Ret.create().setFail();
        if (category.getHelpCount() > 0) return Ret.create().setFail();

        HktSysHelpes helpes = new HktSysHelpes();
        helpes.setCategoryId(hktSysHelpsCategory.getId());

        MongoHelper<HktSysHelpes> hktSysHelpesMongoHelper = new MongoHelper<>();
        List<HktSysHelpes> list = hktSysHelpesMongoHelper.find(helpes, HktSysHelpes.class);
        if (!list.isEmpty()) return Ret.create().setFail().setMsg("该分类下还有文章，不可删除");

        boolean isOk = this.deleteById(category.getId(), category);
        if (isOk) {
            mongoHelper.removeById(category.getId(), HktSysHelpsCategory.class);
            return Ret.create().setOk();
        }
        return Ret.create().setFail();
    }
}
