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

import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.welcome.dto.FriendSettingDetailDTO;
import com.gic.haoban.manage.api.dto.welcome.dto.WelcomeDetailDTO;
import com.gic.haoban.manage.api.dto.welcome.dto.WelcomeListDTO;
import com.gic.haoban.manage.api.dto.welcome.dto.WelcomeMediaDTO;
import com.gic.haoban.manage.api.dto.welcome.dto.WelcomeRemoveCheckDTO;
import com.gic.haoban.manage.api.dto.welcome.dto.WelcomeSuitDepartmentDTO;
import com.gic.haoban.manage.api.dto.welcome.qdto.QueryWelcomeQDTO;
import com.gic.haoban.manage.api.dto.welcome.qdto.SaveFriendSettingQDTO;
import com.gic.haoban.manage.api.dto.welcome.qdto.SaveWelcomeQDTO;
import com.gic.haoban.manage.api.service.out.WelcomeApiService;
import com.gic.haoban.manage.service.errorcode.HaoBanErrCode;
import com.gic.haoban.manage.service.pojo.bo.welcome.FriendSettingDetailBO;
import com.gic.haoban.manage.service.pojo.bo.welcome.SaveFriendSettingBO;
import com.gic.haoban.manage.service.pojo.bo.welcome.SaveWelcomeBO;
import com.gic.haoban.manage.service.pojo.bo.welcome.SaveWelcomeMediaBO;
import com.gic.haoban.manage.service.pojo.bo.welcome.WelcomeDetailBO;
import com.gic.haoban.manage.service.pojo.bo.welcome.WelcomeListBO;
import com.gic.haoban.manage.service.pojo.bo.welcome.WelcomeSuitDepartmentBO;
import com.gic.haoban.manage.service.service.WelcomeService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * desc：欢迎语对外服务接口实现
 *
 * @author: YongEn
 * @date: 2022/2/11
 **/
@Service
public class WelcomeApiServiceImpl implements WelcomeApiService {

    @Autowired
    private WelcomeService welcomeService;

    @Override
    public ServiceResponse<Boolean> existDefaultWelcome(String wxEnterpriseId) {
        return ServiceResponse.success(welcomeService.existDefaultWelcome(wxEnterpriseId));
    }

    @Override
    public ServiceResponse<Boolean> saveWelcome(SaveWelcomeQDTO qdto) {
        SaveWelcomeBO saveWelcomeBO = new SaveWelcomeBO();
        saveWelcomeBO.setWelcomeId(qdto.getWelcomeId());
        saveWelcomeBO.setWxEnterpriseId(qdto.getWxEnterpriseId());
        saveWelcomeBO.setTitle(qdto.getTitle());
        saveWelcomeBO.setWelcomeContent(qdto.getWelcomeContent());
        saveWelcomeBO.setOperatorId(qdto.getOperatorId());
        saveWelcomeBO.setOperatorName(qdto.getOperatorName());
        saveWelcomeBO.setDefaultWelcomeFlag(qdto.getDefaultWelcomeFlag());
        saveWelcomeBO.setCreatorId(qdto.getCreatorId());
        saveWelcomeBO.setCreatorName(qdto.getCreatorName());
        saveWelcomeBO.setModifierId(qdto.getModifierId());
        saveWelcomeBO.setModifierName(qdto.getModifierName());
        saveWelcomeBO.setType(qdto.getType());
        saveWelcomeBO.setScene(qdto.getScene());
        // 媒体列表
        saveWelcomeBO.setWelcomeMediaList(EntityUtil.changeEntityListNew(SaveWelcomeMediaBO.class, qdto.getWelcomeMediaList()));
        // 适用部门新增列表
        saveWelcomeBO.setSuitDepartmentAddList(EntityUtil.changeEntityListNew(WelcomeSuitDepartmentBO.class, qdto.getSuitDepartmentAddList()));
        // 使用部门删除列表
        saveWelcomeBO.setSuitDepartmentDeleteList(EntityUtil.changeEntityListNew(WelcomeSuitDepartmentBO.class, qdto.getSuitDepartmentDeleteList()));
        try {
            Boolean saveWelcome = welcomeService.saveWelcome(saveWelcomeBO);
            return ServiceResponse.success(saveWelcome);
        }catch (RuntimeException ex){
            String message = ex.getMessage();
            return ServiceResponse.failure(HaoBanErrCode.ERR_OTHER.getCode(), message);
        }
    }

    @Override
    public ServiceResponse<Boolean> removeWelcome(String welcomeId) {
        return ServiceResponse.success(
                welcomeService.removeWelcome(welcomeId)
        );
    }

    @Override
    @Deprecated
    public ServiceResponse<List<WelcomeListDTO>> listWelcome(String wxEnterpriseId, String title) {
        List<WelcomeListBO> welcomeList = welcomeService.listWelcome(wxEnterpriseId, title);
        // 数据转换
        List<WelcomeListDTO> welcomeResponseList = convertWelcomeList(welcomeList);
        return ServiceResponse.success(welcomeResponseList);
    }

    private List<WelcomeListDTO> convertWelcomeList(List<WelcomeListBO> welcomeList) {
        return welcomeList.stream().map(one -> {
            WelcomeListDTO dto = new WelcomeListDTO();
            dto.setCreatorId(one.getCreatorId());
            dto.setCreatorName(one.getCreatorName());
            dto.setModifierId(one.getModifierId());
            dto.setModifierName(one.getModifierName());
            dto.setWelcomeId(one.getWelcomeId());
            dto.setWxEnterpriseId(one.getWxEnterpriseId());
            dto.setTitle(one.getTitle());
            dto.setWelcomeContent(one.getWelcomeContent());
            dto.setOpenFlag(one.getOpenFlag());
            dto.setUpdateTime(one.getUpdateTime());
            dto.setOperatorId(one.getOperatorId());
            dto.setOperatorName(one.getOperatorName());
            dto.setDefaultWelcomeFlag(one.getDefaultWelcomeFlag());
            dto.setType(one.getType());
            dto.setScene(one.getScene());
            // 媒体列表
            dto.setWelcomeMediaList(
                    CollectionUtils.isEmpty(one.getWelcomeMediaList()) ? Collections.emptyList()
                            : EntityUtil.changeEntityListNew(WelcomeMediaDTO.class, one.getWelcomeMediaList())
            );
            // 适用部门列表

            dto.setSuitDepartmentList(
                    CollectionUtils.isEmpty(one.getSuitDepartmentList()) ? Collections.emptyList()
                            : EntityUtil.changeEntityListNew(WelcomeSuitDepartmentDTO.class, one.getSuitDepartmentList())
            );
            // 员工暂时不做
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public ServiceResponse<WelcomeDetailDTO> getWelcome(String welcomeId, String enterpriseId) {
        WelcomeDetailBO welcome = welcomeService.getWelcome(welcomeId, enterpriseId);
        // 数据转换
        WelcomeDetailDTO dto = new WelcomeDetailDTO();
        dto.setWelcomeId(welcome.getWelcomeId());
        dto.setWxEnterpriseId(welcome.getWxEnterpriseId());
        dto.setTitle(welcome.getTitle());
        dto.setWelcomeContent(welcome.getWelcomeContent());
        dto.setOpenFlag(welcome.getOpenFlag());
        dto.setDefaultWelcomeFlag(welcome.getDefaultWelcomeFlag());
        dto.setType(welcome.getType());
        dto.setScene(welcome.getScene());
        // 媒体列表
        dto.setWelcomeMediaList(
                CollectionUtils.isEmpty(welcome.getWelcomeMediaList()) ? Collections.emptyList()
                        : EntityUtil.changeEntityListNew(WelcomeMediaDTO.class, welcome.getWelcomeMediaList())
        );
        // 适用部门列表
        dto.setSuitDepartmentList(
                CollectionUtils.isEmpty(welcome.getSuitDepartmentList()) ? Collections.emptyList()
                        : EntityUtil.changeEntityListNew(WelcomeSuitDepartmentDTO.class, welcome.getSuitDepartmentList())
        );
        // 员工暂时不做
        return ServiceResponse.success(dto);
    }

    @Override
    public ServiceResponse<Boolean> saveFriendSetting(SaveFriendSettingQDTO qdto) {
        return ServiceResponse.success(
                welcomeService.saveFriendSetting(EntityUtil.changeEntityNew(SaveFriendSettingBO.class, qdto))
        );
    }

    @Override
    public ServiceResponse<FriendSettingDetailDTO> getFriendSetting(String wxEnterpriseId) {
        FriendSettingDetailBO friendSetting = welcomeService.getFriendSetting(wxEnterpriseId);
        return ServiceResponse.success(
                EntityUtil.changeEntityNew(FriendSettingDetailDTO.class, friendSetting)
        );
    }

    @Override
    public ServiceResponse<Boolean> saveDefaultWelcome(String wxEnterpriseId) {
        return ServiceResponse.success(welcomeService.saveDefaultWelcome(wxEnterpriseId));
    }

    @Override
    public ServiceResponse<Boolean> flushWelcomeMedia() {
        return ServiceResponse.success(welcomeService.flushWelcomeMedia());
    }

    @Override
    public ServiceResponse<Page<WelcomeListDTO>> listWelcomeWithCondition(QueryWelcomeQDTO queryWelcomeQDTO) {
        Page<WelcomeListBO> welcomeListBos = welcomeService.queryWelcomeList(queryWelcomeQDTO);
        List<WelcomeListDTO> welcomeListDTOS = convertWelcomeList(welcomeListBos.getResult());
        ServiceResponse<Page<WelcomeListDTO>> serviceResponse = new ServiceResponse<>();
        Page<WelcomeListDTO> page = new Page<>();
        page.setResult(welcomeListDTOS);
        page.setCurrentPage(welcomeListBos.getCurrentPage());
        page.setPageSize(welcomeListBos.getPageSize());
        page.setTotalPage(welcomeListBos.getTotalPage());
        page.setTotalCount(welcomeListBos.getTotalCount());
        serviceResponse.setResult(page);
        return serviceResponse;
    }

    /**
     * 删除欢迎语，带欢迎语校验
     *
     * @param welcomeId
     * @return
     */
    @Override
    public ServiceResponse<WelcomeRemoveCheckDTO> removeWelcomeWithErrorInfo(String welcomeId) {
        return ServiceResponse.success(welcomeService.removeWithErrorInfo(welcomeId));
    }

    /**
     * 欢迎语被引用统计
     *
     * @param welcomeId
     * @return
     */
    @Override
    public ServiceResponse<WelcomeRemoveCheckDTO> welcomeReferStatistics(String welcomeId) {
        return ServiceResponse.success(welcomeService.welcomeReferStatistics(welcomeId));
    }
}
