package com.jeesharp.business.modules.mall.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.convert.MallPickupPointConvert;
import com.jeesharp.business.modules.mall.dto.MallPickupPointSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallPickupPoint;
import com.jeesharp.business.modules.mall.service.MallPickupPointService;
import com.jeesharp.business.modules.mall.vo.MallPickupPointInfoVO;
import com.jeesharp.business.modules.mall.vo.MallPickupPointListVO;
import com.jeesharp.business.modules.mall.vo.MallPickupPointQueryVO;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 到店自提点 Application Service
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MallPickupPointApplicationService {

    private final MallPickupPointService mallPickupPointService;

    public MallPickupPointInfoVO get(String id) {
        return MallPickupPointConvert.INSTANCE.toInfoVO(mallPickupPointService.get(id));
    }

    public PageRespVO<MallPickupPointListVO> findPage(Page<MallPickupPoint> page, MallPickupPointQueryVO query) {
        Page<MallPickupPoint> result = mallPickupPointService.findPage(page, query);
        return MallPickupPointConvert.INSTANCE.toPageRespVO(result);
    }

    @Transactional(readOnly = false)
    public void save(MallPickupPointSaveDTO dto) {
        MallPickupPoint entity = MallPickupPointConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            mallPickupPointService.createPoint(entity);
            return;
        }
        mallPickupPointService.updatePoint(entity);
    }

    @Transactional(readOnly = false)
    public void delete(String ids) {
        mallPickupPointService.batchDelete(StrUtil.splitTrim(ids, ','));
    }

    /**
     * 获取启用的自提点列表
     */
    public List<MallPickupPoint> listEnabledPoints() {
        return mallPickupPointService.listEnabledPoints();
    }

    /**
     * 根据地区获取自提点
     */
    public List<MallPickupPoint> getPointsByRegion(String province, String city, String district) {
        return mallPickupPointService.getPointsByRegion(province, city, district);
    }

    /**
     * 移动端：按地区筛选启用的自提点列表（省市区均可选，不传则返回全部启用自提点）。
     */
    public List<MallPickupPointListVO> listEnabledListVO(String province, String city, String district) {
        List<MallPickupPointListVO> list = MallPickupPointConvert.INSTANCE.toListVO(
                mallPickupPointService.getPointsByRegion(province, city, district));
        list.forEach(this::fillFullAddress);
        return list;
    }

    private void fillFullAddress(MallPickupPointListVO vo) {
        if (vo == null) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        if (StrUtil.isNotBlank(vo.getProvince())) {
            sb.append(vo.getProvince().trim());
        }
        if (StrUtil.isNotBlank(vo.getCity())) {
            sb.append(vo.getCity().trim());
        }
        if (StrUtil.isNotBlank(vo.getDistrict())) {
            sb.append(vo.getDistrict().trim());
        }
        if (StrUtil.isNotBlank(vo.getAddress())) {
            sb.append(vo.getAddress().trim());
        }
        if (sb.length() > 0) {
            vo.setFullAddress(sb.toString());
        }
    }
}
