package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmMemberOrderDao;
import com.ssm.manage.entity.MemberOrder;
import com.ssm.manage.query.MemberOrderRequest;
import com.ssm.manage.service.SsmMemberOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;

@Service
@Slf4j
public class SsmMemberOrderServiceImpl extends BaseServiceImpl<MemberOrder> implements SsmMemberOrderService {
    @Autowired
    private SsmMemberOrderDao dao;

    @Override
    public SsmMemberOrderDao getDao() {
        return dao;
    }

    @Override
    public PageInfo<MemberOrder> selectPage(MemberOrderRequest memberOrderRequest) {
        return dao.selectPage(memberOrderRequest);
    }


    @Override
    public int addOrder(MemberOrderRequest memberOrderRequest) {
        int i = 0;
        MemberOrder memberOrder = new MemberOrder();
        BeanUtils.copyProperties(memberOrderRequest, memberOrder);
        memberOrder.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
        memberOrder.setInputDate(new Date());
        memberOrder.setState(0);
        i = dao.saveNotNull(memberOrder);
        return i;
    }

    @Override
    public int updateOrder(MemberOrderRequest memberOrderRequest) {
        int i = 0;
        MemberOrder memberOrder = new MemberOrder();
        BeanUtils.copyProperties(memberOrderRequest, memberOrder);
        memberOrder.setUpdateDate(new Date());
        memberOrder.setState(0);
        i = dao.updateByPrimaryKeySelective(memberOrder);
        return i;
    }

    @Override
    public int deleteOrder(MemberOrderRequest memberOrderRequest) {
        int i = 0;
        MemberOrder memberOrder = new MemberOrder();
        BeanUtils.copyProperties(memberOrderRequest, memberOrder);
        memberOrder.setUpdateDate(new Date());
        memberOrder.setState(0);
        i = dao.deleteByPrimaryKey(memberOrder);
        return i;
    }
}
