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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thinkunion.park.service.common.utils.PingYinUtil;
import com.thinkunion.park.service.common.utils.StringGenerator;
import com.yuchen.saas.park.api.dto.property.PropertyAdsPositionQryDto;
import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.entity.property.PropertyAdsPosition;
import com.yuchen.saas.park.api.vo.property.PropertyAdsPositionLeaseListVo;
import com.yuchen.saas.park.api.vo.property.PropertyAdsPositionListVo;
import com.yuchen.saas.park.service.mapper.property.PropertyAdsPositionMapper;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.constant.NestConstant;
import org.nest.springwrap.core.tool.utils.Func;
import org.nest.springwrap.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PropertyAdsPositionService extends BaseServiceImpl<PropertyAdsPositionMapper, PropertyAdsPosition> {
    @Autowired
    private PmgBatParkInfoService pmgBatParkInfoService;
    @Autowired
    private PropertySearchParkInfoService propertySearchParkInfoService;
    @Autowired
    private IPropertyAdsPositionLeaseService propertyAdsPositionLeaseService;
    /**
     * 广告位信息保存
     * @param propertyAdsPosition
     * @return
     */
    public boolean saveAdsPosition(PropertyAdsPosition propertyAdsPosition) {
        this.assemblePropertyAdsPosition(propertyAdsPosition);
        return this.saveOrUpdate(propertyAdsPosition);
    }


    /**
     * 组装保存对象
     * @param propertyAdsPosition
     */
    private void assemblePropertyAdsPosition(PropertyAdsPosition propertyAdsPosition) {
        PmgBatParkInfo parkInfo = pmgBatParkInfoService.getById(propertyAdsPosition.getParkId());
        String pn = PingYinUtil.getPinYinHeadCharUpper(parkInfo.getParkName());
        String resourceCode = pn + "-GGW-" + PingYinUtil.getPinYinHeadCharUpper(propertyAdsPosition.getAdsPositionName());

        LambdaQueryWrapper<PropertyAdsPosition> qw = new LambdaQueryWrapper<>();
        qw.eq(PropertyAdsPosition::getParkId, propertyAdsPosition.getParkId());
        qw.eq(PropertyAdsPosition::getResourceCode, resourceCode);
        PropertyAdsPosition ads = this.getOne(qw);
        if (ads != null) { // 如果已存在拼两位随机数
            resourceCode += StringGenerator.GetRandomString(2);
        }

        propertyAdsPosition.setResourceCode(resourceCode);

    }


    /**
     * 广告位分页查询
     * @param qryDto
     * @return
     */
    public Page<PropertyAdsPositionListVo> selectPage(PropertyAdsPositionQryDto qryDto) {
        Page<PropertyAdsPositionListVo> page = new Page<>(qryDto.getCurrent(), qryDto.getSize());
        page = baseMapper.propertyAdsPositionPage(page, this.getPageQueryParam(qryDto));
        List<PropertyAdsPositionListVo> list = page.getRecords();
        for (PropertyAdsPositionListVo vo : list) {
            PropertyAdsPositionLeaseListVo adsPositionLease = propertyAdsPositionLeaseService.getLeaseByAdsId(vo.getId());
            if (Func.isNotEmpty(adsPositionLease)) {
                vo.setCustomerName(adsPositionLease.getCustomerName());
                vo.setPhone(adsPositionLease.getLinkPhone());
            }
        }
        page.setRecords(list);
        return page;
    }


    /**
     * 分页查询条件
     * @param qryDto
     * @return
     */
    private QueryWrapper<String> getPageQueryParam(PropertyAdsPositionQryDto qryDto) {
        QueryWrapper<String> qw = new QueryWrapper<>();
        if (StringUtil.isNotBlank(qryDto.getAdsPositionName())) {
            qw.like("ap.ads_position_name", qryDto.getAdsPositionName());
        }
        if (Func.isNotEmpty(qryDto.getParkId())) {
            qw.eq("ap.park_id",qryDto.getParkId());
        } else {
            qw.in("ap.park_id",propertySearchParkInfoService.getParkIdsByCurrUser());
        }
        if (Func.isNotBlank(qryDto.getBuildingName())) {
            qw.like("b.building_name",qryDto.getBuildingName());
        }
        if (Func.isNotBlank(qryDto.getResourceCode())) {
            qw.like("ap.resource_code",qryDto.getResourceCode());
        }
        if (Func.isNotEmpty(qryDto.getRentStatus())) {
            qw.like("ap.rent_status",qryDto.getRentStatus());
        }
        qw.eq("ap.is_deleted", NestConstant.DB_NOT_DELETED);
        return qw;
    }

}
