package com.sptg.statistics.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.sptg.statistics.api.entity.StatisticsCount;
import com.sptg.statistics.biz.mapper.StatisticsCountMapper;
import com.sptg.statistics.biz.service.StatisticsCountServiceInterface;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * title: StatisticsCountServiceImpl
 * package: com.sptg.statistics.biz.security.impl
 * description: 统计数量-服务层实现层
 *
 *                
 *                 
 */
@Service
public class StatisticsCountServiceImpl extends ServiceImpl<StatisticsCountMapper, StatisticsCount> implements StatisticsCountServiceInterface{

    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean addCount(StatisticsCount statisticsCount){
        return addStatisticsCount(statisticsCount);
    }

    @LcnTransaction
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean addCountLcn(StatisticsCount statisticsCount){
        return addStatisticsCount(statisticsCount);
    }

    @Override
    public StatisticsCount selectCountOne(String orgPosition){
        //条件
        EntityWrapper<StatisticsCount> wrapper=new EntityWrapper<StatisticsCount>();
        String sqlSelect="SUM(l_ad_exposure_count) AS adExposureCount,SUM(l_ad_push_count) AS adPushCount," +
                "SUM(l_ad_exposure_free_time_count) AS adExposureFreeTimeCount,SUM(l_ad_push_free_time_count) AS adPushFreeTimeCount," +
                "SUM(l_ad_watch_count) AS adWatchCount,SUM(l_ad_watch_user_count) AS adWatchUserCount,SUM(l_ad_watch_visitor_count) AS adWatchVisitorCount," +
                "SUM(l_ad_play_free_time_count) AS adPlayFreeTimeCount,SUM(l_ad_click_mobile_count) AS adClickMobileCount,"+
                " SUM(l_device_count) AS deviceCount,SUM(l_residential_device_count) AS residentialDeviceCount,SUM(l_unit_device_count) AS unitDeviceCount,  " +
                " SUM(l_access_record_count) AS accessRecordCount,SUM(l_access_record_face_count)  AS accessRecordFaceCount" +
                ",SUM(l_access_record_password_count) AS accessRecordPasswordCount,SUM(l_access_record_card_count) AS accessRecordCardCount" +
                ",SUM(l_access_record_telnet_count) AS accessRecordTelnetCount ";
        wrapper.setSqlSelect(sqlSelect);
        wrapper.like("s_org_position",orgPosition, SqlLike.RIGHT);
        StatisticsCount statisticsCount=this.selectOne(wrapper);
        return statisticsCount;
    }

    @Override
    public void updateOrgPosition(String oldPosition, String newPosition){
        this.baseMapper.updateOrgPosition(oldPosition,newPosition);
    }

    /**------------------------------------------------------------------------------------------------------------------**/
    /**-------------------------------------------------内部方法---------------------------------------------------------**/

    /**
     * 功能描述:添加统计数量累加更新字段sql
     * @param addStatisticsCount
     * @return String
     *                
     *                      
     */
    private String getAddCountSetSql(StatisticsCount addStatisticsCount){
        //广告曝光量
        long  adExposureCount=addStatisticsCount.getAdExposureCount();
        //累计发布广告条数
        long adPushCount=addStatisticsCount.getAdPushCount();
        //闲时广告曝光量
        long  adExposureFreeTimeCount=addStatisticsCount.getAdExposureFreeTimeCount();
        //累计发布闲时广告条数
        long adPushFreeTimeCount=addStatisticsCount.getAdPushFreeTimeCount();
        //总观看次数
        long adWatchCount=addStatisticsCount.getAdWatchCount();
        //住户观看人数
        long adWatchUserCount=addStatisticsCount.getAdWatchUserCount();
        //访客观看人数
        long adWatchVisitorCount=addStatisticsCount.getAdWatchVisitorCount();
        //闲时广告播放总次数
        long adPlayFreeTimeCount=addStatisticsCount.getAdPlayFreeTimeCount();
        //移动端广告点击总次数
        long adClickMobileCount=addStatisticsCount.getAdClickMobileCount();
        //设备总数
        long deviceCount=addStatisticsCount.getDeviceCount();
        //小区门口设备总数
        long residentialDeviceCount=addStatisticsCount.getResidentialDeviceCount();
        //单元设备总数
        long unitDeviceCount=addStatisticsCount.getUnitDeviceCount();
        //出入开门次数
        long accessRecordCount=addStatisticsCount.getAccessRecordCount();
        //出入人脸开门次数
        long accessRecordFaceCount=addStatisticsCount.getAccessRecordFaceCount();
        //出入密码开门次数
        long accessRecordPasswordCount=addStatisticsCount.getAccessRecordPasswordCount();
        //出入刷卡开门次数
        long accessRecordCardCount=addStatisticsCount.getAccessRecordCardCount();
        //出入远程开门次数
        long accessRecordTelnetCount=addStatisticsCount.getAccessRecordTelnetCount();
        StringBuffer setStr=new StringBuffer();
        if(adExposureCount>0){
            setStr.append("l_ad_exposure_count=l_ad_exposure_count+"+adExposureCount+",");
        }
        if(adPushCount>0) {
            setStr.append("l_ad_push_count=l_ad_push_count+" + adPushCount + ",");
        }
        if(adExposureFreeTimeCount>0){
            setStr.append("l_ad_exposure_free_time_count=l_ad_exposure_free_time_count+"+adExposureFreeTimeCount+",");
        }
        if(adPushFreeTimeCount>0){
            setStr.append("l_ad_push_free_time_count=l_ad_push_free_time_count+"+adPushFreeTimeCount+",");
        }
        if(adWatchCount>0) {
            setStr.append("l_ad_watch_count=l_ad_watch_count+" + adWatchCount + ",");
        }
        if(adWatchUserCount>0) {
            setStr.append("l_ad_watch_user_count=l_ad_watch_user_count+" + adWatchUserCount + ",");
        }
        if(adWatchVisitorCount>0) {
            setStr.append("l_ad_watch_visitor_count=l_ad_watch_visitor_count+" + adWatchVisitorCount + ",");
        }
        if(adPlayFreeTimeCount>0) {
            setStr.append("l_ad_play_free_time_count=l_ad_play_free_time_count+" + adPlayFreeTimeCount + ",");
        }
        if(adClickMobileCount>0) {
            setStr.append("l_ad_click_mobile_count=l_ad_click_mobile_count+" + adClickMobileCount + ",");
        }
        if(deviceCount>0) {
            setStr.append("l_device_count=l_device_count+" + deviceCount + ",");
        }
        if(residentialDeviceCount>0) {
            setStr.append("l_residential_device_count=l_residential_device_count+" + residentialDeviceCount + ",");
        }
        if(unitDeviceCount>0) {
            setStr.append("l_unit_device_count=l_unit_device_count+" + unitDeviceCount + ",");
        }
        if(accessRecordCount>0) {
            setStr.append("l_access_record_count=l_access_record_count+" + accessRecordCount + ",");
        }
        if(accessRecordFaceCount>0) {
            setStr.append("l_access_record_face_count=l_access_record_face_count+" + accessRecordFaceCount + ",");
        }
        if(accessRecordPasswordCount>0) {
            setStr.append("l_access_record_password_count=l_access_record_password_count+" + accessRecordPasswordCount + ",");
        }
        if(accessRecordCardCount>0) {
            setStr.append("l_access_record_card_count=l_access_record_card_count+" + accessRecordCardCount + ",");
        }
        if(accessRecordTelnetCount>0) {
            setStr.append("l_access_record_telnet_count=l_access_record_telnet_count+" + accessRecordTelnetCount + ",");
        }
        //去掉最后，号
        String setStr2=setStr.substring(0,setStr.lastIndexOf(","));
        return setStr2;
    }

    /**
     * 功能描述:添加统计数量
     * @param addStatisticsCount
     * @return boolean
     *                
     *                      
     */
    private boolean addStatisticsCount(StatisticsCount addStatisticsCount){
        if(StringUtils.isBlank(addStatisticsCount.getOrgPosition())){
            return false;
        }
        EntityWrapper<StatisticsCount> wrapper=new EntityWrapper<StatisticsCount>();
        wrapper.setSqlSelect("l_id AS id");
        wrapper.eq("s_org_position",addStatisticsCount.getOrgPosition());
        StatisticsCount statisticsCount=this.selectOne(wrapper);
        if(statisticsCount!=null){
            String setStr=getAddCountSetSql(addStatisticsCount);
            wrapper.eq("l_id",statisticsCount.getId());
            return this.updateForSet(setStr,wrapper);
        }else{
            return this.insert(addStatisticsCount);
        }
    }


}
