package com.izouma.awesomeadmin.service.impl;

import java.util.*;

import com.izouma.awesomeadmin.dao.AimTemplateArMapper;
import com.izouma.awesomeadmin.dao.ArContentMapper;
import com.izouma.awesomeadmin.model.AimTemplateAr;
import com.izouma.awesomeadmin.model.ArContent;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.AimPlaceInfo;
import com.izouma.awesomeadmin.service.AimPlaceInfoService;
import com.izouma.awesomeadmin.dao.AimPlaceInfoMapper;

/**
 * service接口实现类
 */
@Service
public class AimPlaceInfoServiceImpl implements AimPlaceInfoService {

    private static Logger logger = Logger.getLogger(AimPlaceInfoServiceImpl.class);

    @Autowired
    private AimPlaceInfoMapper aimPlaceInfoMapper;

    @Autowired
    private AimTemplateArMapper aimTemplateArMapper;

    @Autowired
    private ArContentMapper arContentMapper;

    @Override
    public List<AimPlaceInfo> getAimPlaceInfoList(AimPlaceInfo record) {

        logger.info("getAimPlaceInfoList");
        try {

            return aimPlaceInfoMapper.queryAllAimPlaceInfo(record);
        } catch (Exception e) {
            logger.error("getAimPlaceInfoList", e);
        }

        return null;
    }

    @Override
    public List<AimPlaceInfo> getAimPlaceInfoByPage(Page page, AimPlaceInfo record) {

        logger.info("getAimPlaceInfoByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put(AppConstant.PAGE, page);

            return aimPlaceInfoMapper.queryAimPlaceInfoByPage(parameter);
        } catch (Exception e) {
            logger.error("getAimPlaceInfoByPage", e);
        }

        return null;
    }

    @Override
    public AimPlaceInfo getAimPlaceInfoById(String id) {

        logger.info("getAimPlaceInfoyId");
        try {

            return aimPlaceInfoMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            logger.error("getAimPlaceInfoById", e);
        }

        return null;
    }

    @Override
    public AimPlaceInfo getAimPlaceInfo(AimPlaceInfo record) {

        logger.info("getAimPlaceInfo");
        try {

            return aimPlaceInfoMapper.queryAimPlaceInfo(record);
        } catch (Exception e) {
            logger.error("getAimPlaceInfo", e);
        }

        return null;
    }

    @Override
    public boolean createAimPlaceInfo(AimPlaceInfo record) {

        logger.info("createAimPlaceInfo");
        try {

            int updates = aimPlaceInfoMapper.insertSelective(record);

            if (updates > 0) {
                AimTemplateAr param = new AimTemplateAr();
                param.setTemplateAimId(record.getTemplateAimId());

                List<AimTemplateAr> aimTemplateArList = aimTemplateArMapper.queryAllAimTemplateAr(param);
                if (aimTemplateArList.size() > 0) {

                    for (AimTemplateAr aimTemplateAr : aimTemplateArList) {
                        ArContent arContent = new ArContent();
                        arContent.setName(aimTemplateAr.getArName());
                        arContent.setType(aimTemplateAr.getArType());
                        arContent.setActivationMode(aimTemplateAr.getActivationMode());
                        arContent.setAreaId(record.getAreaId());
                        arContent.setThingId(record.getThingId());
                        arContent.setAimGroupId(String.valueOf(record.getAimGroupId()));
                        arContent.setAimPlaceId(String.valueOf(record.getId()));
                        arContent.setTemplateId(aimTemplateAr.getTemplateId());
                        arContent.setTemplateAimId(String.valueOf(aimTemplateAr.getTemplateAimId()));
                        arContent.setTemplateArId(String.valueOf(aimTemplateAr.getId()));

                        arContentMapper.insertSelective(arContent);

                    }
                }
                return true;
            }
        } catch (Exception e) {
            logger.error("createAimPlaceInfo", e);
        }

        return false;
    }

    @Override
    public boolean deleteAimPlaceInfo(AimPlaceInfo record) {

        logger.info("deleteAimPlaceInfo");
        try {

            int updates = aimPlaceInfoMapper.delete(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("deleteAimPlaceInfo", e);
        }

        return false;
    }

    @Override
    public boolean updateAimPlaceInfo(AimPlaceInfo record) {

        logger.info("updateAimPlaceInfo");
        try {

            int updates = aimPlaceInfoMapper.updateByPrimaryKeySelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("updateAimPlaceInfo", e);
        }

        return false;
    }
}

