package com.hwrj.cloud.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.hwrj.cloud.admin.dao.AdvertiseDao;
import com.hwrj.cloud.admin.mapper.SmsHomeAdvertiseMapper;
import com.hwrj.cloud.admin.model.SmsHomeAdvertise;
import com.hwrj.cloud.admin.model.SmsHomeAdvertiseExample;
import com.hwrj.cloud.admin.service.SmsHomeAdvertiseService;
import com.hwrj.cloud.common.exception.ApiException;
import com.hwrj.cloud.common.util.DateCommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

/**
 * 首页广告管理Service实现类
 */
@Service
public class SmsHomeAdvertiseServiceImpl implements SmsHomeAdvertiseService {
    @Autowired
    private SmsHomeAdvertiseMapper advertiseMapper;

    @Autowired
    private AdvertiseDao advertiseDao;

    @Override
    public int create(SmsHomeAdvertise advertise) {
        advertise.setClickCount(0);
        advertise.setOrderCount(0);
        if (advertise.getType()==0){
            checkJumpTime(advertise);
        }
        return advertiseMapper.insert(advertise);
    }

    public boolean checkJumpTime(SmsHomeAdvertise advertise){


        if (advertise.getStartTime()==null||advertise.getEndTime()==null){
            throw new RuntimeException("开始时间为空");
        }

        Boolean gt = DateCommonUtil.whenGT(new Date(), advertise.getEndTime());
        Boolean gt1 = DateCommonUtil.whenGT(advertise.getStartTime(), advertise.getEndTime());
        if (gt||gt1){
            throw new ApiException("结束时间小于现在的时间或者结束时间小于开始时间");
        }

        Integer integer = advertiseDao.checkAdvertiseJumpTime(advertise.getStartTime(), advertise.getEndTime(),advertise.getId());
        if (integer != null&& integer>0){
            throw new ApiException("弹窗时间区间重复");

        }
        return true;
    }

    @Override
    public int delete(List<Long> ids) {
        SmsHomeAdvertiseExample example = new SmsHomeAdvertiseExample();
        example.createCriteria().andIdIn(ids);
        return advertiseMapper.deleteByExample(example);
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        SmsHomeAdvertise record = new SmsHomeAdvertise();
        record.setId(id);
        record.setStatus(status);
        return advertiseMapper.updateByPrimaryKeySelective(record);
    }


    @Override
    public SmsHomeAdvertise getItem(Long id) {
        return advertiseMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(Long id, SmsHomeAdvertise advertise) {
        advertise.setId(id);
        if (advertise.getType()==0){
            checkJumpTime(advertise);
        }
        return advertiseMapper.updateByPrimaryKeySelective(advertise);
    }



    @Override
    public List<SmsHomeAdvertise> list(String name, Integer type, String endTime, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        SmsHomeAdvertiseExample example = new SmsHomeAdvertiseExample();
        SmsHomeAdvertiseExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (!StringUtils.isEmpty(endTime)) {
            String startStr = endTime + " 00:00:00";
            String endStr = endTime + " 23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date start = null;
            try {
                start = sdf.parse(startStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date end = null;
            try {
                end = sdf.parse(endStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (start != null && end != null) {
                criteria.andEndTimeBetween(start, end);
            }
        }
        example.setOrderByClause("sort desc");
        return advertiseMapper.selectByExample(example);
    }
}
