package com.org.hktshop.service.System;

import com.org.hktshop.Interface.system.IHktSysRemindTempleService;
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.HktSysRemindTemple;
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 java.util.List;

@Service
public class HktSysRemindTemplServiceImpl extends BaseService<HktSysRemindTemple> implements IHktSysRemindTempleService {

    @Autowired
    MongoHelper<HktSysRemindTemple> mongoHelper;

    @Override
    public List<HktSysRemindTemple> queryList() {
        List<HktSysRemindTemple> list = mongoHelper.find(new Query(), HktSysRemindTemple.class);
        if (list.isEmpty()) {
            list = this.queryList(new HktSysRemindTemple());
            for (HktSysRemindTemple temple : list) {
                mongoHelper.save(temple);
            }
        }
        return list;
    }

    @Override
    public HktSysRemindTemple queryTemple(HktSysRemindTemple temple) {
        return mongoHelper.findOne(temple, HktSysRemindTemple.class);
    }

    @Override
    public Ret updateTemple(HktAdmin opUser, HktSysRemindTemple temple) {

        if (StringUtils.isNull(temple.getId())) return Ret.create().setFail().set("msg", "id不能为空");
        if (StringUtils.isNull(temple.getTitle())) return Ret.create().setFail().set("msg", "输入不能为空");
        if (StringUtils.isNull(temple.getContent())) return Ret.create().setFail().set("msg", "输入不能为空");

        HktSysRemindTemple remindTemple = new HktSysRemindTemple();
        remindTemple.setId(temple.getId());
        HktSysRemindTemple find = queryTemple(remindTemple);
        if (find == null) return Ret.create().setFail().set("msg", "无相关对象");

        find.setContent(temple.getContent());
        find.setTitle(temple.getTitle());
        find.setModify_time(DateUtil.getDateTime());
        if (this.updateJdbc(find)) {
            mongoHelper.removeById(find.getId(), HktSysRemindTemple.class);
            mongoHelper.save(find);
            return Ret.create().setOk();
        } else {
            return Ret.create().setFail().set("msg", "更新失败");
        }
    }
}
