package com.gic.haoban.manage.service.service.out.impl.hm;

import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.haoban.base.api.common.errCode.HaoBanErrCodeCommon;
import com.gic.haoban.common.utils.PageUtil;
import com.gic.haoban.manage.api.dto.hm.HmWelcomeRelationDTO;
import com.gic.haoban.manage.api.dto.welcome.qdto.QueryWelcomeRelationQDTO;
import com.gic.haoban.manage.api.enums.WelcomeRelationStatusEnum;
import com.gic.haoban.manage.api.enums.hm.HmWelcomeReferType;
import com.gic.haoban.manage.api.service.hm.HmWelcomeRelationApiService;
import com.gic.haoban.manage.service.pojo.bo.hm.HmWelcomeRelationBO;
import com.gic.haoban.manage.service.pojo.qo.WelcomeRelationQO;
import com.gic.haoban.manage.service.service.hm.WelcomeRelationService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @Author MUSI
 * @Date 2022/7/4 10:52 AM
 * @Description
 * @Version
 **/
@Component(value = "hmWelcomeRelationApiServiceImpl")
public class HmWelcomeRelationApiServiceImpl implements HmWelcomeRelationApiService {

    @Autowired
    private WelcomeRelationService welcomeRelationService;

    /**
     * 条件查询 欢迎语关联关系
     *
     * @param queryWelcomeRelationQdto
     * @return
     */
    @Override
    public ServiceResponse<Page<HmWelcomeRelationDTO>> queryWelcomeRelation(QueryWelcomeRelationQDTO queryWelcomeRelationQdto) {
        if (queryWelcomeRelationQdto.getWelcomeId() == null){
            return ServiceResponse.failure(HaoBanErrCodeCommon.ERR_2.getCode()+"", HaoBanErrCodeCommon.ERR_2.getMsg());
        }
        WelcomeRelationQO welcomeRelationQo = new WelcomeRelationQO();
        welcomeRelationQo.setWelcomeId(queryWelcomeRelationQdto.getWelcomeId());
        welcomeRelationQo.setStatusList(Arrays.asList(WelcomeRelationStatusEnum.ENABLE.getCode(), WelcomeRelationStatusEnum.DEPRECATED.getCode()));
        welcomeRelationQo.setSearchContent(queryWelcomeRelationQdto.getSearchContent());
        welcomeRelationQo.setPageNum(queryWelcomeRelationQdto.getPageNum());
        welcomeRelationQo.setPageSize(queryWelcomeRelationQdto.getPageSize());
        Page<HmWelcomeRelationBO> welcomeRelationPage = welcomeRelationService.queryWelcomeRelationPage(welcomeRelationQo);
        Page<HmWelcomeRelationDTO> welcomeRelationDTOPage = PageUtil.changeToCurrentPage(welcomeRelationPage, HmWelcomeRelationDTO.class);
        List<HmWelcomeRelationDTO> result = welcomeRelationDTOPage.getResult();
        if (!CollectionUtils.isEmpty(result)){
            for (HmWelcomeRelationDTO welcomeRelationBO : result) {
                if (HmWelcomeReferType.LINK.getCode().equals(welcomeRelationBO.getType())){
                    // 链接状态不展示
                    welcomeRelationBO.setStatus(null);
                }
            }
        }
        welcomeRelationDTOPage.setResult(result);
        return ServiceResponse.success(welcomeRelationDTOPage);
    }
}
