package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.jz.db.dao.JzMallNoticeMapper;
import org.linlinjava.jz.db.domain.JzMallNotice;
import org.linlinjava.jz.db.domain.JzMallNoticeExample;
import org.linlinjava.jz.db.dao.JzMallNoticeMapper;
import org.linlinjava.jz.db.domain.JzMallNotice;
import org.linlinjava.jz.db.domain.JzMallNoticeAdmin;
import org.linlinjava.jz.db.domain.JzMallNoticeAdminExample;
import org.linlinjava.jz.db.domain.JzMallNoticeExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class JzMallNoticeService {
    @Resource
    private JzMallNoticeMapper noticeMapper;


    public List<JzMallNotice> querySelective(String title, String content, Integer page, Integer limit, String sort, String order) {
        JzMallNoticeExample example = new JzMallNoticeExample();
        JzMallNoticeExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtils.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return noticeMapper.selectByExample(example);
    }

    public int updateById(JzMallNotice notice) {
        notice.setUpdateTime(LocalDateTime.now());
        return noticeMapper.updateByPrimaryKeySelective(notice);
    }

    public void deleteById(Integer id) {
        noticeMapper.logicalDeleteByPrimaryKey(id);
    }

    public int add(JzMallNotice notice) {
        notice.setAddTime(LocalDateTime.now());
        notice.setUpdateTime(LocalDateTime.now());
        return noticeMapper.insertSelective(notice);
    }

    public JzMallNotice findById(Integer id) {
        return noticeMapper.selectByPrimaryKey(id);
    }

    public void deleteByIds(List<Integer> ids) {
        JzMallNoticeExample example = new JzMallNoticeExample();
        example.or().andIdIn(ids).andDeletedEqualTo(false);
        JzMallNotice notice = new JzMallNotice();
        notice.setUpdateTime(LocalDateTime.now());
        notice.setDeleted(true);
        noticeMapper.updateByExampleSelective(notice, example);
    }
}
