package com.dgtly.wxportal.controller;


import com.dgtly.common.annotation.ApiPassToken;
import com.dgtly.common.core.controller.ApiBaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.core.domain.ParameterObject;
import com.dgtly.system.domain.AlertConfiguration;
import com.dgtly.system.domain.AlertLog;
import com.dgtly.system.domain.SysUserExt;
import com.dgtly.system.service.IAlertConfigurationService;
import com.dgtly.system.service.IAlertLogService;
import com.dgtly.system.service.ISysUserExtService;
import com.dgtly.system.service.ISysUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 弹框日志Controller
 *
 * @author chenyn
 * @date 2023-06-13
 */
@Api(tags = "微信弹框相关接口")
@RestController
@RequestMapping("/wxAlert")
@ApiPassToken
public class WxAlertLogController  extends ApiBaseController {
    @Autowired
    private IAlertLogService alertLogService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysUserExtService sysUserExtService;
    @Autowired
    private IAlertConfigurationService alertConfigurationService;


    @ApiOperation(value = "微信弹框",notes = "参数：{userId:1}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params" , paramType = "body")
    })
    @PostMapping("/getAlert")
    public Object getAlertLog() throws ParseException {
        String userType="";
        ParameterObject obj = getParameterObject();
        Long userId = Long.valueOf(obj.getString("userId"));
        SysUserExt sysUserExt = sysUserExtService.selectSysUserExtById(userId);
        String salesLevel = sysUserExt.getSalesLevel();
        if(("customer_level").equals(salesLevel) ){
            userType = "1";
        }else{
            userType = "0";
        }
        //是否有启动
        AlertConfiguration alertConfiguration1 = alertConfigurationService.selectAlertConfigurationByAlertStatus(userType,sysUserExt.getSalesLevel(),sysUserExt.getOrgCode());

        if (alertConfiguration1!=null){
            //有启动
            alertConfiguration1.setIsClearCache(false);
            Date datez = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String now = sdf.format(datez);
            Date date = sdf.parse(now);
            int i = date.compareTo(alertConfiguration1.getAlertStartTime());
            int i1 = date.compareTo(alertConfiguration1.getAlertEndTime());
            if (i >= 0 && i1 < 0) {
                //满足时间
                String[] split = alertConfiguration1.getUserType().split(",");
                List<String> strings = Arrays.asList(split);

                if ((strings.contains("1")  || strings.contains("2")) &&sysUserExt.getSalesLevel().equals("customer_level")){
                    //满足类型对应a
                    AlertLog alertLog = alertLogService.selectAlertLogByUserIdAndAlertId(userId, alertConfiguration1.getAlertId(),"0");
                    //AlertLog log = alertLogService.selectAlertLogByUserIdAndAlertId(userId, alertConfiguration1.getAlertId(),null);
                  /*  if(log == null){
                        alertConfiguration1.setIsClearCache(true);
                    }*/
                    if (alertLog!=null){
                        if (alertLog.getAlertNum()<alertConfiguration1.getAlertNum()){
                            Long count =alertLog.getAlertNum()+1;
                            // 创建两个日期对象
                            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                            Date date1 = dateFormat.parse(sdf.format(alertLog.getCreateTime()));
                            Date date2 = dateFormat.parse(sdf.format(alertConfiguration1.getAlertEndTime()));
                            // 设置时间部分为零点
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(date1);
                            calendar.set(Calendar.HOUR_OF_DAY, 0);
                            calendar.set(Calendar.MINUTE, 0);
                            calendar.set(Calendar.SECOND, 0);
                            calendar.set(Calendar.MILLISECOND, 0);
                            date1 = calendar.getTime();
                            calendar.setTime(date2);
                            calendar.set(Calendar.HOUR_OF_DAY, 0);
                            calendar.set(Calendar.MINUTE, 0);
                            calendar.set(Calendar.SECOND, 0);
                            calendar.set(Calendar.MILLISECOND, 0);
                            date2 = calendar.getTime();
                            int clearT = date1.compareTo(date2);
                            if(count == alertConfiguration1.getAlertNum() && clearT==0){
                                alertConfiguration1.setIsClearCache(true);
                                alertLog.setIsClearCache("1");
                            }
                            alertLog.setAlertNum(count);
                            alertLogService.updateAlertLog(alertLog);
                            return AjaxResult.success(alertConfiguration1);
                        }else {
                            return AjaxResult.warning("每日弹出次数已满");
                        }
                    }else {
                        //加数据
                        AlertLog alertLog1 = new AlertLog();
                        alertLog1.setAlertId(alertConfiguration1.getAlertId());
                        alertLog1.setUserId(userId);
                        alertLog1.setUserType("1");
                        alertLog1.setAlertNum(1L);
                        // 创建两个日期对象
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        Date date1 = dateFormat.parse(now);
                        Date date2 = dateFormat.parse(sdf.format(alertConfiguration1.getAlertEndTime()));
                        // 设置时间部分为零点
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(date1);
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        date1 = calendar.getTime();
                        calendar.setTime(date2);
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        date2 = calendar.getTime();
                        int clearT = date1.compareTo(date2);
                        if(clearT==0 && alertConfiguration1.getAlertNum()==1){
                            alertConfiguration1.setIsClearCache(true);
                            alertLog1.setIsClearCache("1");
                        }
                        alertLogService.insertAlertLog(alertLog1);
                        return AjaxResult.success(alertConfiguration1);
                    }
                }
                if((strings.contains("0") || strings.contains("2")) && !sysUserExt.getSalesLevel().equals("customer_level")){
                    AlertLog alertLog = alertLogService.selectAlertLogByUserIdAndAlertId(userId, alertConfiguration1.getAlertId(),"0");
                  /*  AlertLog log = alertLogService.selectAlertLogByUserIdAndAlertId(userId, alertConfiguration1.getAlertId(),null);
                    if(log == null){
                        alertConfiguration1.setIsClearCache(true);
                    }*/
                    if (alertLog!=null){
                        if (alertLog.getAlertNum()<alertConfiguration1.getAlertNum()){
                            Long count =alertLog.getAlertNum()+1;
                            // 创建两个日期对象
                            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                            Date date1 = dateFormat.parse(sdf.format(alertLog.getCreateTime()));
                            Date date2 = dateFormat.parse(sdf.format(alertConfiguration1.getAlertEndTime()));
                            // 设置时间部分为零点
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(date1);
                            calendar.set(Calendar.HOUR_OF_DAY, 0);
                            calendar.set(Calendar.MINUTE, 0);
                            calendar.set(Calendar.SECOND, 0);
                            calendar.set(Calendar.MILLISECOND, 0);
                            date1 = calendar.getTime();

                            calendar.setTime(date2);
                            calendar.set(Calendar.HOUR_OF_DAY, 0);
                            calendar.set(Calendar.MINUTE, 0);
                            calendar.set(Calendar.SECOND, 0);
                            calendar.set(Calendar.MILLISECOND, 0);
                            date2 = calendar.getTime();
                            int clearT = date1.compareTo(date2);
                            if(count == alertConfiguration1.getAlertNum() && clearT==0){
                                alertConfiguration1.setIsClearCache(true);
                                alertLog.setIsClearCache("1");
                            }
                            alertLog.setAlertNum(count);
                            alertLogService.updateAlertLog(alertLog);
                            return AjaxResult.success(alertConfiguration1);
                        }else {
                            return AjaxResult.warning("每日弹出次数已满");
                        }
                    }else {
                        //加数据
                        AlertLog alertLog1 = new AlertLog();
                        alertLog1.setAlertId(alertConfiguration1.getAlertId());
                        alertLog1.setUserId(userId);
                        alertLog1.setUserType("0");
                        alertLog1.setAlertNum(1L);
                        // 创建两个日期对象
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        Date date1 = dateFormat.parse(now);
                        Date date2 = dateFormat.parse(sdf.format(alertConfiguration1.getAlertEndTime()));
                        // 设置时间部分为零点
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(date1);
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        date1 = calendar.getTime();
                        calendar.setTime(date2);
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        date2 = calendar.getTime();
                        int clearT = date1.compareTo(date2);
                        if(clearT==0 && alertConfiguration1.getAlertNum()==1){
                            alertConfiguration1.setIsClearCache(true);
                            alertLog1.setIsClearCache("1");
                        }
                        alertLogService.insertAlertLog(alertLog1);
                        return AjaxResult.success(alertConfiguration1);
                    }
                }
                return AjaxResult.warning("没有符合的弹框");

            } else {
                //超时
                return AjaxResult.warning("时间超时");
            }
        }else {
            //无启动
            return AjaxResult.warning("没有启动的弹框");
        }
    }


}
