package com.iotechn.unimall.admin.api.sys;


import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.model.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.data.domain.CustomerBillingDetailDO;
import com.iotechn.unimall.data.dto.CustomerBillingDetailDTO;
import com.iotechn.unimall.data.mapper.CustomerBillingDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;

/**
 * Generate Code By Unimall
 */
@Service
public class customerBillingDetailServiceImpl implements customerBillingDetailService {

    @Autowired
    private CustomerBillingDetailMapper customerBillingDetailMapper;

    @Override
    public boolean delete(Long id,  Long adminId) throws ServiceException {
        return customerBillingDetailMapper.deleteById(id) > 0;
    }

    @Override
    public Page<CustomerBillingDetailDO> list(CustomerBillingDetailDTO selectDTO, Integer page, Integer limit, Long adminId) throws ServiceException {
        Wrapper<CustomerBillingDetailDO> wrapper = new EntityWrapper<CustomerBillingDetailDO>();
        List<CustomerBillingDetailDO> list = customerBillingDetailMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = customerBillingDetailMapper.selectCount(wrapper);
        return new Page<CustomerBillingDetailDO>(list, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CustomerBillingDetailDO create(CustomerBillingDetailDTO insertDTO, Long adminId) throws ServiceException {
         Date now = new Date();
         CustomerBillingDetailDO insertDO = new CustomerBillingDetailDO();
         BeanUtils.copyProperties(insertDTO, insertDO);
         insertDO.setGmtUpdate(now);
         insertDO.setGmtCreate(now);
         if (customerBillingDetailMapper.insert(insertDO) > 0) {
             return insertDO;
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(CustomerBillingDetailDTO editDTO,Long adminId) throws ServiceException {
         Date now = new Date();
         CustomerBillingDetailDO updateDO = new CustomerBillingDetailDO();
         BeanUtils.copyProperties(editDTO, updateDO);
         updateDO.setGmtUpdate(now);
         if (customerBillingDetailMapper.updateById(updateDO) > 0) {
             return "ok";
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }


}
