package cn.iocoder.yudao.module.system.service.rental;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.customer.RentalCustomerPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.customer.RentalCustomerSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.customer.RentalCustomerStatusReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.rental.RentalCustomerDO;
import cn.iocoder.yudao.module.system.dal.mysql.rental.RentalCustomerMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.RENTAL_CUSTOMER_NOT_EXISTS;

/**
 * 租赁客户 Service 实现类
 *
 * @author 符溶馨
 */
@Service
@Validated
public class RentalCustomerServiceImpl implements RentalCustomerService {

    @Resource
    private RentalCustomerMapper rentalCustomerMapper;

    @Override
    public Long createRentalCustomer(RentalCustomerSaveReqVO createReqVO) {
        // 插入
        RentalCustomerDO rentalCustomer = BeanUtils.toBean(createReqVO, RentalCustomerDO.class);
        rentalCustomer.setStatus(CommonStatusEnum.ENABLE.getStatus());
        rentalCustomerMapper.insert(rentalCustomer);
        // 返回
        return rentalCustomer.getId();
    }

    @Override
    public void updateRentalCustomer(RentalCustomerSaveReqVO updateReqVO) {
        // 校验存在
        validateRentalCustomerExists(updateReqVO.getId());
        // 更新
        RentalCustomerDO updateObj = BeanUtils.toBean(updateReqVO, RentalCustomerDO.class);
        rentalCustomerMapper.updateById(updateObj);
    }

    @Override
    public void updateRentalCustomerStatus(RentalCustomerStatusReqVO updateReqVO) {
        // 校验存在
        validateRentalCustomerExists(updateReqVO.getId());
        // 更新
        RentalCustomerDO updateObj = BeanUtils.toBean(updateReqVO, RentalCustomerDO.class);
        rentalCustomerMapper.updateById(updateObj);
    }

    @Override
    public void updateRentalCustomerAmount(Long id, BigDecimal amount) {
        // 校验存在
        validateRentalCustomerExists(id);
        // 更新
        rentalCustomerMapper.updateById(new RentalCustomerDO().setId(id).setAmount(amount));
    }

    @Override
    public void deleteRentalCustomer(Long id) {
        // 校验存在
        validateRentalCustomerExists(id);
        // 删除
        rentalCustomerMapper.deleteById(id);
    }

    private void validateRentalCustomerExists(Long id) {
        if (rentalCustomerMapper.selectById(id) == null) {
            throw exception(RENTAL_CUSTOMER_NOT_EXISTS);
        }
    }

    @Override
    public RentalCustomerDO getRentalCustomer(Long id) {
        return rentalCustomerMapper.selectById(id);
    }

    @Override
    public PageResult<RentalCustomerDO> getRentalCustomerPage(RentalCustomerPageReqVO pageReqVO) {
        return rentalCustomerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RentalCustomerDO> getRentalCustomerList() {
        return rentalCustomerMapper.selectList(RentalCustomerDO::getStatus, CommonStatusEnum.ENABLE.getStatus());
    }

    @Override
    public List<RentalCustomerDO> getListByFactory(Long userId) {
        return rentalCustomerMapper.selectListByFactory(userId);
    }

}