package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.entity.ActivityEnroll;
import com.lightinit.hsdataportal.entity.ActivityWithBLOBs;
import com.lightinit.hsdataportal.mapper.ActivityEnrollMapper;
import com.lightinit.hsdataportal.mapper.ActivityMapper;
import com.lightinit.hsdataportal.model.ActivityModel;
import com.lightinit.hsdataportal.model.ActivitySearchModel;
import com.lightinit.hsdataportal.model.ResultState;
import com.lightinit.hsdataportal.model.ResultStateCode;
import com.lightinit.hsdataportal.service.IInnovationActivityService;
import com.lightinit.hsdataportal.spring.DynamicDataContextHolder;
import org.apache.ibatis.javassist.tools.rmi.Sample;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.text.resources.FormatData;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Service
public class InnovationActivityServiceImpl implements IInnovationActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityEnrollMapper activityEnrollMapper;
    @Override
    public List<ActivityWithBLOBs> getList(ActivitySearchModel activitySearchModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Date date = new Date();

//        if(activitySearchModel.getActivityTime()=="今天") {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            String date1 =  sdf.format(date);
//            activitySearchModel.setActivityTime(date1);
//        }
//        if(activitySearchModel.getActivityTime()=="明天") {
//            Calendar calendar = new GregorianCalendar();
//            calendar.setTime(date);
//            calendar.add(Calendar.DAY_OF_MONTH, +1);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            String date1 =  sdf.format(date);
//            activitySearchModel.setActivityTime(date1);
//
//        }
//        if(activitySearchModel.getActivityTime()=="最近7天") {
//            Calendar calendar = new GregorianCalendar();
//            calendar.setTime(date);
//            calendar.add(Calendar.DAY_OF_MONTH, -7);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            String date1 =  sdf.format(date);
//            activitySearchModel.setActivityTime(date1);
//
//        }
//        if(activitySearchModel.getActivityTime()=="最近30天") {
//            Calendar calendar = new GregorianCalendar();
//            calendar.setTime(date);
//            calendar.add(Calendar.DAY_OF_MONTH, -30);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            String date1 =  sdf.format(date);
//            activitySearchModel.setActivityTime(date1);
//
//        }
        List<ActivityWithBLOBs> list = activityMapper.getList(activitySearchModel);
        return list;
    }

    @Override
    public long getCount(ActivitySearchModel activitySearchModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return activityMapper.getCount(activitySearchModel);
    }

    @Override
    public ResultState toAdd(ActivityModel activity) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState();
        int result = 0;
        ActivityWithBLOBs activityWithBLOBs = new ActivityWithBLOBs();
        BeanUtils.copyProperties(activity,activityWithBLOBs);
        result = activityMapper.insert(activityWithBLOBs);
        if(result<0) {
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("发布活动失败");
        }
        resultState.setMsg("发布活动成功");

        return resultState;
    }

    @Override
    public ActivityWithBLOBs view(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ActivityWithBLOBs activity = activityMapper.selectByPrimaryKey(id);
        return activity;
    }

    @Override
    public ResultState enroll(ActivityEnroll activityEnroll) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState();
        int result = 0;
        result = activityEnrollMapper.insert(activityEnroll);
        if(!(result>0)) {
            resultState.setMsg("报名失败");
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
        }
        resultState.setData(activityEnroll.getId());
        return resultState;
    }
}
