package com.zcloud.basic.info.facade;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.zcloud.basic.info.api.CorpInfoServiceI;
import com.zcloud.basic.info.command.convertor.CorpInfoCoConvertor;
import com.zcloud.basic.info.domain.enums.CorpTypeEnum;
import com.zcloud.basic.info.dto.CorpInfoAddCmd;
import com.zcloud.basic.info.dto.CorpInfoPageQry;
import com.zcloud.basic.info.dto.CorpInfoUpdateCmd;
import com.zcloud.basic.info.dto.clientobject.CorpInfoCO;
import com.zcloud.basic.info.dto.clientobject.DepartmentCO;
import com.zcloud.gbscommon.zcloudcorp.face.ZcloudCorpFacade;
import com.zcloud.gbscommon.zcloudcorp.request.ZcloudCorpInfoAddCmd;
import com.zcloud.gbscommon.zcloudcorp.request.ZcloudCorpInfoPageQry;
import com.zcloud.gbscommon.zcloudcorp.request.ZcloudCorpInfoUpdateCmd;
import com.zcloud.gbscommon.zcloudcorp.response.ZcloudCorpInfoCO;
import com.zcloud.gbscommon.zclouddepartment.response.ZcloudDepartmentInfoCo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

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

@DubboService
public class ZcloudCorpFacadeImpl implements ZcloudCorpFacade {
    @Resource
    private CorpInfoServiceI corpInfoService;
    @Resource
    private CorpInfoCoConvertor corpInfoCoConvertor;
    @Override
    public SingleResponse<ZcloudCorpInfoCO> getCorpInfoById(Long id) {
        CorpInfoCO info = corpInfoService.info(id);
        ZcloudCorpInfoCO zcloudCorpInfoCO = new ZcloudCorpInfoCO();
        BeanUtils.copyProperties(info, zcloudCorpInfoCO);
        return SingleResponse.of(zcloudCorpInfoCO);
    }

    @Override
    public PageResponse<ZcloudCorpInfoCO> pageByParentId(ZcloudCorpInfoPageQry zcloudCorpInfoPageQry) {
        CorpInfoPageQry qry = new CorpInfoPageQry();
        BeanUtils.copyProperties(zcloudCorpInfoPageQry, qry);
        PageResponse<CorpInfoCO> corpInfoCOPageResponse = corpInfoService.listPage(qry);
        List<ZcloudCorpInfoCO> zcloudPage = corpInfoCoConvertor.converCOsToDubboCOs(corpInfoCOPageResponse.getData());
        return PageResponse.of(zcloudPage, corpInfoCOPageResponse.getTotalCount(), corpInfoCOPageResponse.getPageSize(), corpInfoCOPageResponse.getPageIndex());
    }

    @Override
    public SingleResponse addCorp(ZcloudCorpInfoAddCmd zcloudCorpInfoAddCmd) {
        CorpInfoAddCmd addCmd =new CorpInfoAddCmd();
        BeanUtils.copyProperties(zcloudCorpInfoAddCmd, addCmd);
        return corpInfoService.add(addCmd);
    }

    @Override
    public SingleResponse editCorp(ZcloudCorpInfoUpdateCmd zcloudCorpInfoUpdateCmd) {
        CorpInfoUpdateCmd updateCmd =new CorpInfoUpdateCmd();
        BeanUtils.copyProperties(zcloudCorpInfoUpdateCmd, updateCmd);
        corpInfoService.edit(updateCmd);
        return SingleResponse.buildSuccess();
    }

    @Override
    public MultiResponse<ZcloudCorpInfoCO> getCorpList() {
        List<CorpInfoCO> list = corpInfoService.getCorpList();
        List<ZcloudCorpInfoCO> zcloudCorpCo = corpInfoCoConvertor.converCOsToDubboCOs(list);
        return MultiResponse.of(zcloudCorpCo);
    }

    @Override
    public MultiResponse<ZcloudCorpInfoCO> listByCorpIds(Collection<Long> collection) {
        List<CorpInfoCO> list = corpInfoService.listByCorpIds(collection);
        List<ZcloudCorpInfoCO> zcloudCorpCo = corpInfoCoConvertor.converCOsToDubboCOs(list);
        return MultiResponse.of(zcloudCorpCo);
    }

    @Override
    public SingleResponse<Boolean> verifyCorpInfo() {
        return SingleResponse.of(corpInfoService.verifyCorpInfo());
    }

    @Override
    public MultiResponse<Integer> listCorpType(Integer enterpriseType) {
        return MultiResponse.of(CorpTypeEnum.getCodesByEnterpriseType(enterpriseType));
    }

}
