package cn.superdesk.uniorder.dubbo.provider.organisation;

import cn.superdesk.data.extract.enums.opCenter.OpCenterBizPlatformEnum;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dto.param.operationCenter.base.OperationCenterParam;
import cn.superdesk.uniorder.dto.param.organisation.SysEnterpriseQueryCriteria;
import cn.superdesk.uniorder.dto.param.organisation.SysOrgQueryParam;
import cn.superdesk.uniorder.dto.param.organisation.SysVirtQueryParam;
import cn.superdesk.uniorder.dto.vo.operationCenter.business.BusinessVo;
import cn.superdesk.uniorder.dto.vo.operationCenter.customer.OpCenterCustomerOperatorOrgVo;
import cn.superdesk.uniorder.dto.vo.sys.SysOrganizationXEnterpriseVo;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.organisation.SysEnterprise;
import cn.superdesk.uniorder.dto.param.organisation.SysEnterpriseParam;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionVirt;
import cn.superdesk.uniorder.dto.vo.sys.SysSelectVo;
import cn.superdesk.uniorder.dto.vo.sys.SysVirtVo;
import cn.superdesk.uniorder.dubbo.facade.organisation.DubboSysEnterpriseService;
import cn.superdesk.uniorder.service.operationCenter.OperationCenterCustomerService;
import cn.superdesk.uniorder.service.organisation.SysEnterpriseService;
import cn.superdesk.uniorder.service.organisation.SysOrganizationService;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service(version = "1.0.0", retries = -1, timeout = 10000)
@Slf4j
public class DubboSysEnterpriseServiceImpl implements DubboSysEnterpriseService {


	@Autowired
	private SysEnterpriseService sysEnterpriseService;


	@Autowired
	private OperationCenterCustomerService operationCenterCustomerService;


	@Autowired
	private SysOrganizationService sysOrganizationService;


	/****************************************** extends BaseService 部分 start ******************************************/

	@Override
	public void addObj(SysEnterpriseParam param) {

		SysEnterprise sysEnterprise = AnBeanUtils.copy(param, SysEnterprise.class);
		sysEnterpriseService.insertSelective(sysEnterprise);

	}

	@Override
	public void updateObj(SysEnterpriseParam param) {

		SysEnterprise sysEnterprise = AnBeanUtils.copy(param, SysEnterprise.class);
		sysEnterpriseService.updateByPrimaryKeySelectiveWithRet(sysEnterprise);
	}

	@Override
	public void deleteObj(Long id) {
		sysEnterpriseService.deleteObj(id);
	}

	@Override
	public SysEnterprise findById(Long id) {
		return sysEnterpriseService.findById(id);
	}

	@Override
	public PageInfo<SysEnterprise> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
		return sysEnterpriseService.findByPaging(conditions, pageNun, pageSize);
	}

	@Override
	public List<SysOrganizationXEnterpriseVo> findSysOrganizationXEnterprise(Map<String, Object> queryMap) {

		return sysEnterpriseService.findSysOrganizationXEnterprise(queryMap);
	}

	/****************************************** extends BaseService 部分   end ******************************************/


	@Override
	public void addSysEnterprise(SysEnterpriseParam param) {

		SysEnterprise sysEnterprise = AnBeanUtils.copy(param, SysEnterprise.class);
		sysEnterpriseService.addSysEnterprise(sysEnterprise);
	}

	@Override
	public void updateSysEnterprise(SysEnterpriseParam param) {

		SysEnterprise sysEnterprise = AnBeanUtils.copy(param, SysEnterprise.class);
		sysEnterpriseService.updateSysEnterprise(sysEnterprise);
	}

	@Override
	public void deleteSysEnterprise(Long id) {
		sysEnterpriseService.deleteSysEnterprise(id);
	}

	@Override
	public SysEnterprise findSysEnterpriseById(Long id) {
		return sysEnterpriseService.findSysEnterpriseById(id);
	}

	@Override
	public R<List<SysEnterprise>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {
		return sysEnterpriseService.pageQuery(conditions, pageNun, pageSize);

	}

	@Override
	public R<List<SysOrganizationXEnterpriseVo>> pageQuery(SysEnterpriseQueryCriteria queryCriteria, LoginSysUser loginSysUser) {
		return sysEnterpriseService.pageQuery(queryCriteria, loginSysUser);
	}

	/****************************************** 之前的 要合并 start ******************************************/


	@Override
	public void add(SysEnterprise t) {

		sysEnterpriseService.insertSelective(t);
	}

	@Override
	public void update(SysEnterprise t) {

		sysEnterpriseService.updateByPrimaryKeySelectiveWithRet(t);
	}


	@Override
	public List<SysEnterprise> findByName(Long orgId, String enterpriseName) {
		return sysEnterpriseService.findByVirtName(orgId, enterpriseName);
	}

	@Override
	public R<List<SelectOptionVirt>> getVirtSelect(Map<String, Object> params, int pageNum, int pageSize) {

		return sysEnterpriseService.getVirtSelect(params, pageNum, pageSize);
	}

	@Override
	public R<List<SelectOptionVirt>> getVirtSelectByOutOrgId(Long outOrgId, int pageNum, int pageSize) {


		return sysEnterpriseService.getVirtSelectByOutOrgId(outOrgId, pageNum, pageSize);
	}

	@Override
	public SysEnterprise findByVirtId(Long id) {

		SysEnterprise sysEnterprise = sysEnterpriseService.findByOutVirtId(id);

		if (sysEnterprise != null)
			return sysEnterprise;

		return null;
	}

	@Override
	public List<SysEnterprise> findByOutVirtIds(List<Long> outVirtIds) {
		return sysEnterpriseService.findByOutVirtIds(outVirtIds);
	}

	@Override
	public Map<Long, SysEnterprise> findByOutVirtIdsConvertKV(List<Long> outVirtIds) {
		return sysEnterpriseService.findByOutVirtIdsConvertKV(outVirtIds);
	}

	@Override
	public List<String> convertOutVirtIds2VirtNames(List<Long> outVirtIds) {
		return sysEnterpriseService.convertOutVirtIds2VirtNames(outVirtIds);
	}

	@Override
	public void addSpecDaiDeLiangHang(Long id) {

		sysEnterpriseService.addSpecDaiDeLiangHang(id);
	}

	@Override
	public void deleteDaiDeLiangHang(Long id) {

		sysEnterpriseService.deleteDaiDeLiangHang(id);
	}


	@Override
	public void mergeReceiveVirtualarchitecture4SuperdeskMall(String message) {
		sysEnterpriseService.mergeReceiveVirtualarchitecture4SuperdeskMall(message);
	}

	@Override
	public void deleteReceiveVirtualarchitecture4SuperdeskMall(String message) {
		sysEnterpriseService.deleteReceiveVirtualarchitecture4SuperdeskMall(message);
	}


	@Override
	public void mergeReceiveVirtualarchitecture4SuperdeskTransfer(String message) {
		sysEnterpriseService.mergeReceiveVirtualarchitecture4SuperdeskTransfer(message);
	}

	@Override
	public void deleteReceiveVirtualarchitecture4SuperdeskTransfer(String message) {
		sysEnterpriseService.deleteReceiveVirtualarchitecture4SuperdeskTransfer(message);
	}

	/**
	 * 同步集采企业数据
	 * @param message
	 */
	@Override
	public void mergeReceiveVirtualarchitecture4Qchjc(String message) {
		sysEnterpriseService.mergeReceiveVirtualarchitecture4Qchjc(message);
	}

	@Override
	public R<List<SysVirtVo>> getVirtList(SysVirtQueryParam param) {
		PageHelper.startPage(param.getPageNumber(), param.getPageSize());
		List<SysVirtVo> list = sysEnterpriseService.getVirtList(param);
		PageInfo<SysVirtVo> page = new PageInfo<>(list);
		return new R<List<SysVirtVo>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), list);
	}

	@Override
	public List<SysSelectVo> selectList(OperationCenterParam param) {
		//根据项目
		Map<String, Object> conditions = new HashMap<>();
		conditions.put("customerId", param.getCustomerId());
		if (StringUtils.isNotBlank(param.getOperatorIds())) {
			conditions.put("operatorIds", param.getOperatorIds());
		}


		//先查询关联的项目
		List<OpCenterCustomerOperatorOrgVo> operatorOrgList = operationCenterCustomerService.getLinkOperatorAndOrg(conditions);
		List<Long> orgIdList = new ArrayList<>();


		//根据客户，运营商，查询项目 orgIdList->关联的项目 orgIds->传的项目
		//循环 查看该客户的权限-项目 判断如果 有一条关联全部项目
		SysOrgQueryParam paramQuery = new SysOrgQueryParam();
		for (OpCenterCustomerOperatorOrgVo vo : operatorOrgList) {
			if (vo.getIsAllOrg()==1) {
				// 全部项目--关联该运营商下的全部项目
				paramQuery.setCompId(vo.getBizOperatorId().toString());
				paramQuery.setBizPlatformCode(vo.getBizPlatformCode());
				List<SysSelectVo> sysOrgIdList = sysOrganizationService.selectOrgListByPlatform(paramQuery);
				orgIdList.addAll(sysOrgIdList.stream().map(x -> x.getId()).collect(Collectors.toList()));

			}else {//部分项目

				orgIdList.add(vo.getOrgId());
			}
		}
		log.info("客户ID为:{},查询关联的项目{}",param.getCustomerId(), JSON.toJSONString(orgIdList));

		if (StringUtils.isNotBlank(param.getOrgIds())) {
			conditions.put("orgIds", param.getOrgIds());
		}
		if (CollectionUtils.isNotEmpty(orgIdList)) {
			conditions.put("orgIdList", orgIdList);
			return sysEnterpriseService.selectList(conditions);
		}

		return new ArrayList<>();

	}

	/****************************************** 之前的 要合并   end ******************************************/

}
