package com.goafanti.news.service.impl;

import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.JtNewsMapper;
import com.goafanti.common.error.BusinessException;
import com.goafanti.common.model.JtNews;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.news.bo.InputJtNews;
import com.goafanti.news.bo.OutJtNews;
import com.goafanti.news.service.EventPlanningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public  class EventPlanningServiceImpl extends BaseMybatisDao<JtNewsMapper> implements EventPlanningService {

    @Autowired
    private JtNewsMapper    jtNewsMapper;

    @Override
    public int add(InputJtNews in) {
        if (in.getReleaseTimes()!=null) {
            try {
                in.setReleaseTime(DateUtils.parseDate(in.getReleaseTimes(), AFTConstants.YYYYMMDDHHMMSS));
            } catch (ParseException e) {
                throw new BusinessException("转换异常");
            }
        }
        if (in.getReleaseStatus()==1&&in.getReleaseTime()==null) {
            in.setReleaseTime(new Date());
        }

        return jtNewsMapper.insertSelective(in);
    }

    @Override
    public int update(InputJtNews in) {
        if (in.getReleaseTimes()!=null) {
            try {

                in.setReleaseTime(DateUtils.parseDate(in.getReleaseTimes(), AFTConstants.YYYYMMDDHHMMSS));
            } catch (ParseException e) {
                throw new BusinessException("转换异常");
            }
        }

        if (in.getReleaseStatus()!=null&&in.getReleaseStatus()==1&&in.getReleaseTime()==null) {
            in.setReleaseTime(new Date());
        }

        return jtNewsMapper.updateByPrimaryKeySelective(in);
    }

    @Override
    public Pagination<OutJtNews> listnewsDetails(InputJtNews in) {
        Map<String, Object> params = new HashMap<>();
        params.put("n", in);
        return (Pagination<OutJtNews>) findPage("findNewsList", "findNewsCount", params, in.getPageNo(),in.getPageSize());
    }

    @Override
    public OutJtNews selectNews(InputJtNews in) {
        return jtNewsMapper.selectByid(in.getId());
    }
}
