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

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.statistics.api.dto.SysDeviceLogDto;
import com.sptg.statistics.api.entity.SysDeviceLog;
import com.sptg.statistics.biz.mapper.SysDeviceLogMapper;
import com.sptg.statistics.biz.service.SysDeviceLogServiceInterface;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * title: SysAdviceServiceImpl
 * package: com.sptg.statistics.biz.security.impl
 * description: 机器事件日志-服务层
 *
 *                
 *                 
 */
@Service
public class SysDeviceLogServiceImpl extends ServiceImpl<SysDeviceLogMapper, SysDeviceLog> implements SysDeviceLogServiceInterface {

    @Override
    public Page<SysDeviceLog> getSysDeviceLogList(SysDeviceLogDto sdlDto) {
        Page<SysDeviceLog> page=sdlDto.getPage();
        //条件
        EntityWrapper<SysDeviceLog> wrapper=new EntityWrapper<SysDeviceLog>();
        //序列号
        String serialNumber=sdlDto.getSerialNumber();
        //机器事件
        Integer malfunctionEvent = sdlDto.getEvent();
        //开始故障时间条件
        Date startMalfunctionDate=sdlDto.getStartMalfunctionDate();
        //结束故障时间条件
        Date endMalfunctionDate=sdlDto.getEndMalfunctionDate();
        //机构位置
        String orgPosition=sdlDto.getOrgPosition();
        if(StringUtils.isNotBlank(serialNumber)){
            wrapper.like("s_serial_number",serialNumber);
        }
        if(malfunctionEvent!=null){
            wrapper.eq("i_event",malfunctionEvent);
        }
        if(startMalfunctionDate != null){
            wrapper.ge("t_create_date",startMalfunctionDate);
        }
        if(endMalfunctionDate != null){
            wrapper.le("s_message",endMalfunctionDate);
        }
        wrapper.eq("i_status", OperationConstant.ZERO);
        //根据机构
        if(StringUtils.isNotBlank(orgPosition)) {
            wrapper.like("s_org_position", orgPosition, SqlLike.RIGHT);
        }
        //排序
        wrapper.orderBy("i_event",false);
        return this.selectPage(page,wrapper);
    }

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

    @Override
    public List<Map<String,Object>> statisticsDeviceMalfunction(SysDeviceLogDto sdlDto){
        EntityWrapper<SysDeviceLog> wrapper=new EntityWrapper<>();
        String sqlSelect="SUM(1) AS deviceCount,DATE_FORMAT(t_create_date,'%m') AS month";
        String year=sdlDto.getYear();
        wrapper.setSqlSelect(sqlSelect);
        //省
        String province=sdlDto.getRegionProvince();
        if(StringUtils.isNotBlank(province)){
            wrapper.eq("s_region_province",province);
        }
        //市
        String city=sdlDto.getRegionCity();
        if(StringUtils.isNotBlank(city)){
            wrapper.eq("s_region_city",city);
        }
        //区
        String area=sdlDto.getRegionArea();
        if(StringUtils.isNotBlank(area)){
            wrapper.eq("s_region_area",area);
        }
        //故障状态设备
        wrapper.eq("i_event", OperationConstant.MINUS_ONE);
        //状态
        wrapper.eq("i_status", OperationConstant.ZERO);
        //按年查询
        if(StringUtils.isNotBlank(year)){
            wrapper.eq("DATE_FORMAT(t_create_date,'%Y')",year);
        }
        //分组
        wrapper.groupBy("month");
        return this.selectMaps(wrapper);
    }

    @Override
    public List<Map<String,Object>> getDeviceFaultCountList(SysDeviceLogDto sdlDto){
        EntityWrapper<SysDeviceLog> wrapper=new EntityWrapper<>();
        String sqlSelect = null;
        //按日查询
        if(OperationConstant.ZERO == sdlDto.getSelectStatus()){
            sqlSelect="SUM(1) AS indexCount,DATE_FORMAT(t_create_date,'%H') AS indexDate";
            wrapper.setSqlSelect(sqlSelect);
            wrapper.eq("DATE_FORMAT(t_create_date,'%Y-%m-%d')",sdlDto.getSelectCon());
        }
        //本周
        if(OperationConstant.ONE == sdlDto.getSelectStatus()){
            sqlSelect="SUM(1) AS indexCount,DATE_FORMAT(t_create_date,'%w') AS indexDate";
            wrapper.setSqlSelect(sqlSelect);
            wrapper.where("YEARWEEK(date_format(t_create_date,'%Y-%m-%d'),1) = YEARWEEK(now(),1)");
        }
        //按月查询
        if(OperationConstant.TWO == sdlDto.getSelectStatus()){
            sqlSelect="SUM(1) AS indexCount,DATE_FORMAT(t_create_date,'%d') AS indexDate";
            wrapper.setSqlSelect(sqlSelect);
            wrapper.eq("DATE_FORMAT(t_create_date,'%Y-%m')",sdlDto.getSelectCon());
        }
        //按年查询
        if(OperationConstant.THREE == sdlDto.getSelectStatus()){
            sqlSelect="SUM(1) AS indexCount,DATE_FORMAT(t_create_date,'%m') AS indexDate";
            wrapper.setSqlSelect(sqlSelect);
            wrapper.eq("DATE_FORMAT(t_create_date,'%Y')",sdlDto.getSelectCon());
        }
        //故障状态设备
        wrapper.eq("i_event", OperationConstant.MINUS_ONE);
        //状态
        wrapper.eq("i_status", OperationConstant.ZERO);
        //权限
        wrapper.like("s_org_position", sdlDto.getOrgPosition(),SqlLike.RIGHT);
        //分组
        wrapper.groupBy("indexDate");
        return this.selectMaps(wrapper);
    }

    @Override
    public List<Map<String, Object>> deviceRankForArea(SysDeviceLogDto sdlDto) {
        List<Map<String,Object>> deviceMap=null;
        EntityWrapper<SysDeviceLog> wrapper= new EntityWrapper<>();
        String sqlSelect="COUNT(1) AS deviceCount,s_region_province AS regionProvince";
        wrapper.setSqlSelect(sqlSelect);
        //排序
        wrapper.orderBy("deviceCount",false);
        //故障状态设备
        wrapper.eq("i_event", OperationConstant.MINUS_ONE);
        //状态
        wrapper.eq("i_status", OperationConstant.ZERO);
        //机构位置
        wrapper.like("s_org_position", sdlDto.getOrgPosition(), SqlLike.RIGHT);
        //按省份分组
        wrapper.groupBy("s_region_province");
        wrapper.isNotNull("s_region_province");
        //排序
        wrapper.orderBy("deviceCount",false);
        //分页查询
        Page page=sdlDto.getPage();
        if(page!=null){
            Page<Map<String, Object>> mapPage = this.selectMapsPage(page, wrapper);
            deviceMap=mapPage.getRecords();
        }else{
            deviceMap=this.selectMaps(wrapper);
        }
        return deviceMap;
    }
}
