package com.yuchen.saas.park.service.service.property.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.property.PropertyAdsPositionLeaseDto;
import com.yuchen.saas.park.api.dto.property.PropertyAdsPositionLeaseQueryDto;
import com.yuchen.saas.park.api.entity.property.PropertyAdsPositionLease;
import com.yuchen.saas.park.api.entity.property.PropertyCustomerArchives;
import com.yuchen.saas.park.api.vo.property.PropertyAdsPositionLeaseListVo;
import com.yuchen.saas.park.service.mapper.property.PropertyAdsPositionLeaseMapper;
import com.yuchen.saas.park.service.service.property.*;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.tool.utils.BeanUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author 蔡文强
 * @Date 2024/1/2 10:56
 * @Description 广告位租赁表 服务实现类
 */
@Service
public class PropertyAdsPositionLeaseServiceImpl extends BaseServiceImpl<PropertyAdsPositionLeaseMapper, PropertyAdsPositionLease> implements IPropertyAdsPositionLeaseService {

    @Resource
    private IPropertyCustomerArchivesService customerArchivesService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveorUpdateLease(PropertyAdsPositionLeaseDto leaseDto) {
        PropertyAdsPositionLease propertyAdsPositionLease = BeanUtil.copyProperties(leaseDto, PropertyAdsPositionLease.class);
        PropertyCustomerArchives customerArchives = customerArchivesService.getCustomerArchivesById(leaseDto.getCustomerArchivesId());
        customerArchives.setLinkManOne(leaseDto.getLinkMan());
        customerArchives.setLinkPhoneOne(leaseDto.getLinkPhone());
        customerArchivesService.updateById(customerArchives);
        if (Func.isNotEmpty(propertyAdsPositionLease.getId())) {
            return updateById(propertyAdsPositionLease);
        }
        return save(propertyAdsPositionLease);
    }

    @Override
    public Boolean deleteLease(Long id) {
        return removeById(id);
    }

    @Override
    public NestPage<PropertyAdsPositionLeaseListVo> getLeasePageList(PropertyAdsPositionLeaseQueryDto queryDto) {
        LambdaQueryWrapper<PropertyAdsPositionLease> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyAdsPositionLease::getAdsId, queryDto.getAdsId());
        IPage<PropertyAdsPositionLease> page = this.page(Condition.getPage(queryDto), queryWrapper);
        List<PropertyAdsPositionLeaseListVo> list = BeanUtil.copyProperties(page.getRecords(), PropertyAdsPositionLeaseListVo.class);
        for (PropertyAdsPositionLeaseListVo vo : list) {
            PropertyCustomerArchives customerArchives = customerArchivesService.getCustomerArchivesById(vo.getCustomerArchivesId());
            vo.setCustomerName(customerArchives.getCustomerName());
            vo.setLinkMan(customerArchives.getLinkManOne());
            vo.setLinkPhone(customerArchives.getLinkPhoneOne());
        }
        IPage<PropertyAdsPositionLeaseListVo> result = new Page<>(queryDto.getCurrent(), queryDto.getSize(), page.getTotal());
        result.setRecords(list);
        return NestPage.of(result);
    }

    @Override
    public PropertyAdsPositionLeaseListVo getLeaseByAdsId(Long adsId) {
        PropertyAdsPositionLease propertyAdsPositionLease = this.lambdaQuery().eq(PropertyAdsPositionLease::getAdsId, adsId)
                .orderByDesc(PropertyAdsPositionLease::getCreateTime).last("LIMIT 1").one();
        PropertyAdsPositionLeaseListVo vo = BeanUtil.copyProperties(propertyAdsPositionLease, PropertyAdsPositionLeaseListVo.class);
        if (Func.isNotEmpty(propertyAdsPositionLease)) {
            PropertyCustomerArchives customerArchives = customerArchivesService.getCustomerArchivesById(vo.getCustomerArchivesId());
            vo.setCustomerName(customerArchives.getCustomerName());
            vo.setLinkMan(customerArchives.getLinkManOne());
            vo.setLinkPhone(customerArchives.getLinkPhoneOne());
        }
        return vo;
    }

}
