package com.yc.ship.module.otc.api.link;


import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.otc.api.link.LinkApi;
import com.yc.ship.module.otc.api.link.dto.LinkDetailRespDTO;
import com.yc.ship.module.otc.api.link.dto.LinkRespDTO;
import com.yc.ship.module.otc.dal.dataobject.link.LinkDO;
import com.yc.ship.module.otc.dal.dataobject.link.LinkDetailDO;
import com.yc.ship.module.otc.dal.dataobject.store.StoreDO;
import com.yc.ship.module.otc.dal.dataobject.store.StoreUserDO;
import com.yc.ship.module.otc.service.link.LinkDetailService;
import com.yc.ship.module.otc.service.link.LinkService;
import com.yc.ship.module.otc.service.store.StoreService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

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

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception0;
import static com.yc.ship.framework.common.util.collection.CollectionUtils.convertList;


/**
 * &#064;Description 旅行社授权接口
 * &#064;Author qsl
 * &#064;Date 2025/6/4 15:28
 */
@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class LinkApiImpl implements LinkApi {

    @Resource
    private LinkService linkService;

    @Resource
    private LinkDetailService linkDetailService;

    @Resource
    private StoreService storeService;

    @Override
    public CommonResult<LinkRespDTO> getLinkById(Long id) {
        LinkDO linkDO = linkService.getLink(id);
        if (linkDO == null) {
            throw exception0(30_004, "链接不存在！");
        }
        LinkRespDTO linkRespDTO = BeanUtils.toBean(linkDO, LinkRespDTO.class);
        List<LinkDetailDO> detailList = linkDetailService.getLinkDetailByLinkId(id);
        List<LinkDetailRespDTO> detailRespDTOS = new ArrayList<>(detailList.size());
        LinkDetailRespDTO linkDetailRespDTO;
        // 链接类型 1.门店码 2.售票员码
        List<Long> sourceCodes = convertList(detailList, LinkDetailDO::getSourceCode);
        if (linkDO.getType() == 1) {
            // 只查门店
            List<StoreDO> storeList = storeService.getSotreList(sourceCodes);
            for (StoreDO storeDO : storeList) {
                linkDetailRespDTO = new LinkDetailRespDTO();
                linkDetailRespDTO.setStoreId(storeDO.getId());
                linkDetailRespDTO.setStoreName(storeDO.getStoreName());
                detailRespDTOS.add(linkDetailRespDTO);
            }
        }
        if (linkDO.getType() == 2) {
            // 先查售票员 再查门店
            List<StoreUserDO> storeUserList = storeService.getStoreUserList(2, sourceCodes);
            List<StoreDO> storeList = storeService.getSotreList(convertList(storeUserList, StoreUserDO::getStoreId));
            for (StoreUserDO storeUserDO : storeUserList) {
                linkDetailRespDTO = new LinkDetailRespDTO();
                linkDetailRespDTO.setUserId(storeUserDO.getUserId());
                linkDetailRespDTO.setStoreId(storeUserDO.getStoreId());
                for (StoreDO storeDO : storeList) {
                    if (storeDO.getId().equals(storeUserDO.getStoreId())) {
                        linkDetailRespDTO.setStoreName(storeDO.getStoreName());
                        break;
                    }
                }
                detailRespDTOS.add(linkDetailRespDTO);
            }
        }
        linkRespDTO.setDetailRespDTOS(detailRespDTOS);
        return CommonResult.success(linkRespDTO);
    }
}
