package com.gic.haoban.manage.service.service.moment.impl;

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.DateUtil;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.api.dto.moment.QwMomentPlanDTO;
import com.gic.haoban.manage.api.qdto.moment.QwMomentPlanQDTO;
import com.gic.haoban.manage.service.dao.mapper.moment.QwMomentPlanMapper;
import com.gic.haoban.manage.service.entity.moment.TabQwMomentPlan;
import com.gic.haoban.manage.service.service.moment.QwMomentPlanService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service("qwMomentPlanService")
public class QwMomentPlanServiceImpl implements QwMomentPlanService {

    private final Logger logger = LogManager.getLogger(this.getClass());
    @Autowired
    private QwMomentPlanMapper qwMomentPlanMapper ;

    @Override
    public ServiceResponse<QwMomentPlanDTO> save(QwMomentPlanDTO dto) {
        logger.info("保存朋友圈活动={}", JSONObject.toJSONString(dto));
        Long planId = dto.getPlanId() ;
        TabQwMomentPlan plan = EntityUtil.changeEntity(TabQwMomentPlan.class,dto) ;
        Date now = new Date() ;
        plan.setUpdateTime(now);
        // 1立即发布2定时发布
        if(plan.getPublishType()==1) {
            plan.setPublishTime(now);
        }
        plan.setBeginDate(plan.getPublishTime());
        Date endTime = DateUtil.addDay(plan.getBeginDate(), plan.getExpireDays());
        plan.setEndDate(endTime);
        if(null == planId) {
            planId = UniqueIdUtils.uniqueLong();
            dto.setPlanId(planId);
            plan.setPlanId(planId);
            plan.setCreateTime(now);
            plan.setStatusFlag(1);
            this.qwMomentPlanMapper.insert(plan);
        }else {
            this.qwMomentPlanMapper.update(plan);
        }

        return ServiceResponse.success(dto);
    }

    @Override
    public ServiceResponse<Void> del(Long planId) {
        this.qwMomentPlanMapper.del(planId);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Void> stop(Long planId) {
        this.qwMomentPlanMapper.stop(planId);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<QwMomentPlanDTO> detail(Long planId) {
        TabQwMomentPlan plan = this.qwMomentPlanMapper.getById(planId);
        QwMomentPlanDTO dto = EntityUtil.changeEntityByJSON(QwMomentPlanDTO.class,plan) ;
        return ServiceResponse.success(dto);
    }

    @Override
    public ServiceResponse<Page<QwMomentPlanDTO>> planList(QwMomentPlanQDTO qdto) {
        return null;
    }


}
