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

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.rentalsalerecord.RentalSaleRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.rentalsalerecord.RentalSaleRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.rentalsalerecord.RentalSaleRecordSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.rentalsalerecord.RentalSaleStatusReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.rental.RentalCustomerDO;
import cn.iocoder.yudao.module.system.dal.dataobject.rental.RentalDepositRecordDO;
import cn.iocoder.yudao.module.system.dal.dataobject.rental.RentalSaleRecordDO;
import cn.iocoder.yudao.module.system.dal.mysql.rental.RentalCustomerMapper;
import cn.iocoder.yudao.module.system.dal.mysql.rental.RentalSaleRecordMapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
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_SALE_RECORD_NOT_EXISTS;

/**
 * 租赁转销售记录 Service 实现类
 *
 * @author 符溶馨
 */
@Service
@Validated
public class RentalSaleRecordServiceImpl implements RentalSaleRecordService {

    @Resource
    private RentalSaleRecordMapper rentalSaleRecordMapper;

    @Resource
    private RentalCustomerMapper customerMapper;

    @Resource
    private RentalDepositRecordService depositRecordService;

    @Override
    public Long createRentalSaleRecord(RentalSaleRecordSaveReqVO createReqVO) {
        // 插入
        RentalSaleRecordDO rentalSaleRecord = BeanUtils.toBean(createReqVO, RentalSaleRecordDO.class);
        rentalSaleRecordMapper.insert(rentalSaleRecord);
        // 返回
        return rentalSaleRecord.getId();
    }

    @Override
    public void createBatch(List<RentalSaleRecordSaveReqVO> createReqVO) {

        // 批量插入
        List<RentalSaleRecordDO> rentalSaleDO = BeanUtils.toBean(createReqVO, RentalSaleRecordDO.class);
        rentalSaleRecordMapper.insertBatch(rentalSaleDO);
    }

    @Override
    public void updateRentalSaleRecord(RentalSaleStatusReqVO updateReqVO) {
        // 校验存在
        validateRentalSaleRecordExists(updateReqVO.getId());
        // 更新
        RentalSaleRecordDO updateObj = BeanUtils.toBean(updateReqVO, RentalSaleRecordDO.class);
        rentalSaleRecordMapper.updateById(updateObj);

        if (updateReqVO.getStatus() == 1) {

            // 同步更新客户押金
            customerMapper.update(null, new LambdaUpdateWrapper<RentalCustomerDO>()
                    .setSql("amount = amount + " + updateReqVO.getAmount())
                    .eq(RentalCustomerDO::getId, updateReqVO.getCustomerId()));

            // 同步更新入账类型
            RentalDepositRecordDO depositRecordDO = new RentalDepositRecordDO()
                    .setId(updateReqVO.getDepositId())
                    .setType(1);
            depositRecordService.updateRentalDepositRecordType(Collections.singletonList(depositRecordDO));
        }
    }

    @Override
    public void deleteRentalSaleRecord(Long id) {
        // 校验存在
        validateRentalSaleRecordExists(id);
        // 删除
        rentalSaleRecordMapper.deleteById(id);
    }

    private void validateRentalSaleRecordExists(Long id) {
        if (rentalSaleRecordMapper.selectById(id) == null) {
            throw exception(RENTAL_SALE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public RentalSaleRecordDO getRentalSaleRecord(Long id) {
        return rentalSaleRecordMapper.selectById(id);
    }

    @Override
    public PageResult<RentalSaleRecordDO> getRentalSaleRecordPage(RentalSaleRecordPageReqVO pageReqVO) {
        return rentalSaleRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RentalSaleRecordRespVO> getSaleDepositRecordList() {
        return rentalSaleRecordMapper.selectSaleDepositRecordList();
    }
}