package com.izouma.awesomeadmin.service.impl;

import java.util.*;

import com.izouma.awesomeadmin.dao.AimPlaceInfoMapper;
import com.izouma.awesomeadmin.dao.AimTemplateMapper;
import com.izouma.awesomeadmin.dao.ArContentMapper;
import com.izouma.awesomeadmin.model.*;
import javafx.scene.shape.Arc;
import org.apache.commons.lang.StringUtils;
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.service.AimGroupInfoService;
import com.izouma.awesomeadmin.dao.AimGroupInfoMapper;

/**
 * service接口实现类
 */
@Service
public class AimGroupInfoServiceImpl implements AimGroupInfoService {

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

    @Autowired
    private AimGroupInfoMapper aimGroupInfoMapper;

    @Autowired
    private AimTemplateMapper aimTemplateMapper;

    @Autowired
    private AimPlaceInfoMapper aimPlaceInfoMapper;

    @Autowired
    private ArContentMapper arContentMapper;

    @Override
    public List<AimGroupInfo> getAimGroupInfoList(AimGroupInfo record) {

        logger.info("getAimGroupInfoList");
        try {

            return aimGroupInfoMapper.queryAllAimGroupInfo(record);
        } catch (Exception e) {
            logger.error("getAimGroupInfoList", e);
        }

        return null;
    }

    @Override
    public List<AimGroupInfo> getAimGroupInfoByPage(Page page, AimGroupInfo record) {

        logger.info("getAimGroupInfoByPage");
        try {

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

            return aimGroupInfoMapper.queryAimGroupInfoByPage(parameter);
        } catch (Exception e) {
            logger.error("getAimGroupInfoByPage", e);
        }

        return null;
    }

    @Override
    public AimGroupInfo getAimGroupInfoById(String id) {

        logger.info("getAimGroupInfoyId");
        try {

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

        return null;
    }

    @Override
    public AimGroupInfo getAimGroupInfo(AimGroupInfo record) {

        logger.info("getAimGroupInfo");
        try {

            return aimGroupInfoMapper.queryAimGroupInfo(record);
        } catch (Exception e) {
            logger.error("getAimGroupInfo", e);
        }

        return null;
    }

    @Override
    public boolean createAimGroupInfo(AimGroupInfo record) {

        logger.info("createAimGroupInfo");
        try {

            int updates = aimGroupInfoMapper.insertSelective(record);

            if (updates > 0) {

                //生成预设点标
                if (StringUtils.isNotEmpty(record.getAimTemplateId())) {
                    AimTemplate aimTemplate = new AimTemplate();
                    aimTemplate.setId(Integer.valueOf(record.getAimTemplateId()));
                    aimTemplate = aimTemplateMapper.queryAimTemplateAllInfo(aimTemplate);

                    if (aimTemplate != null) {
                        List<AimTemplateAim> aimTemplateAimList = aimTemplate.getAimTemplateAimList();
                        if (aimTemplateAimList.size() > 0) {
                            for (AimTemplateAim aimTemplateAim : aimTemplateAimList) {
                                AimPlaceInfo aimPlaceInfo = new AimPlaceInfo();
                                aimPlaceInfo.setName(aimTemplateAim.getAimName());
                                aimPlaceInfo.setIcon(aimTemplateAim.getIcon());
                                aimPlaceInfo.setIconRank(aimTemplateAim.getIconRank());
                                aimPlaceInfo.setAloneFlag(aimTemplateAim.getAloneFlag());
                                aimPlaceInfo.setAutoPlay(aimTemplateAim.getAutoPlay());
                                aimPlaceInfo.setAutoDisplay(aimTemplateAim.getAutoDisplay());
                                aimPlaceInfo.setShowLevel(aimTemplateAim.getShowLevel());
                                aimPlaceInfo.setTemplateId(record.getAimTemplateId());
                                aimPlaceInfo.setTemplateAimId(String.valueOf(aimTemplateAim.getId()));
                                aimPlaceInfo.setOwnedLandMarkId(record.getLandmarkId());
                                aimPlaceInfo.setAreaId(record.getAreaId());
                                aimPlaceInfo.setThingId(record.getThingId());
                                aimPlaceInfo.setAimGroupId(String.valueOf(record.getId()));

                                aimPlaceInfoMapper.insertSelective(aimPlaceInfo);

                                if (aimPlaceInfo.getId() != null) {
                                    List<AimTemplateAr> aimTemplateArList = aimTemplateAim.getAimTemplateArList();
                                    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.getId()));
                                            arContent.setAimPlaceId(String.valueOf(aimPlaceInfo.getId()));
                                            arContent.setTemplateId(record.getAimTemplateId());
                                            arContent.setTemplateAimId(String.valueOf(aimTemplateAim.getId()));
                                            arContent.setTemplateArId(String.valueOf(aimTemplateAr.getId()));

                                            arContentMapper.insertSelective(arContent);

                                        }
                                    }
                                }

                            }


                        }

                    }


                }

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

        return false;
    }

    @Override
    public boolean deleteAimGroupInfo(AimGroupInfo record) {

        logger.info("deleteAimGroupInfo");
        try {

            int updates = aimGroupInfoMapper.delete(record);

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

        return false;
    }

    @Override
    public boolean updateAimGroupInfo(AimGroupInfo record) {

        logger.info("updateAimGroupInfo");
        try {

            int updates = aimGroupInfoMapper.updateByPrimaryKeySelective(record);

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

        return false;
    }
}

