package com.yihu.hos.qc.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.config.MongoConfig;
import com.yihu.hos.core.log.Logger;
import com.yihu.hos.core.log.LoggerFactory;
import com.yihu.hos.qc.dao.RuleDao;
import com.yihu.hos.qc.model.RuleModel;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zbus.broker.ZbusBroker;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author CD
 * @vsrsion 1.0
 * Created at 2017/05/05.
 */
@Service("ruleService")
public class RuleService {
    public static final String BEAN_ID = "ruleService";
    static final Logger logger = LoggerFactory.getLogger(RuleService.class);
    @Resource(name = RuleDao.BEAN_ID)
    private RuleDao ruleDao;

    @Value("${spring.data.mongodb.gridFsDatabase}")
    private String dbName;

    @Autowired
    private MongoConfig mongoConfig;

    @Autowired
    private ObjectMapper objectMapper;

    private ZbusBroker zbusBroker;

    @Autowired
    public void setZbusBroker(ZbusBroker zbusBroker) {
        this.zbusBroker = zbusBroker;
    }


    public Result getRuleList(Map<String, Object> params) throws Exception {
        return ruleDao.getRuleList(params);
    }


    public RuleModel getRuleById(String id) throws Exception {
        return ruleDao.getEntity(RuleModel.class, id);
    }

    @Transactional
    public Result addRule(RuleModel obj) throws Exception {
        ruleDao.saveEntity(obj);
        return Result.success("保存成功");
    }

    @Transactional
    public Result updateRule(RuleModel obj) throws Exception {
        RuleModel ruleModel = ruleDao.getEntity(RuleModel.class, obj.getId());
        ruleModel.setName(obj.getName());
        ruleModel.setType(obj.getType());
        ruleModel.setDescribe(obj.getDescribe());
        ruleModel.setRule(obj.getRule());
        ruleModel.setError_code(obj.getError_code());
        return Result.success("更新成功");
    }

    @Transactional
    public Result deleteRule(String id) throws Exception {
        RuleModel ruleModel = ruleDao.getEntity(RuleModel.class, id);
        ruleDao.deleteEntity(ruleModel);
        return Result.success("删除成功");
    }

    /* ==============================服务模块=================================  */


    public Result getRuleServiceList(Map<String, Object> params) throws Exception {
        return ruleDao.getRuleList(params);
    }

}
