package com.liquidnet.client.admin.zhengzai.smile.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.common.utils.StringUtils;
import com.liquidnet.client.admin.common.utils.bean.BeanUtils;
import com.liquidnet.client.admin.zhengzai.smile.dto.SmileOrgAndUserVo;
import com.liquidnet.client.admin.zhengzai.smile.dto.SmileOrganizationParam;
import com.liquidnet.client.admin.zhengzai.smile.dto.SmileOrganizationVo;
import com.liquidnet.client.admin.zhengzai.smile.dto.SmileUserVoOrg;
import com.liquidnet.client.admin.zhengzai.smile.service.ISmileOrganizationService;
import com.liquidnet.commons.lang.util.BeanUtil;
import com.liquidnet.service.smile.entity.SmileOrganization;
import com.liquidnet.service.smile.entity.SmileUser;
import com.liquidnet.service.smile.mapper.SmileOrganizationMapper;
import com.liquidnet.service.smile.mapper.SmileUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2022-03-14
 */
@Slf4j
@Service
public class SmileOrganizationServiceImpl extends ServiceImpl<SmileOrganizationMapper, SmileOrganization> implements ISmileOrganizationService {

    @Autowired
    SmileOrganizationMapper smileOrganizationMapper;

    @Autowired
    SmileUserMapper smileUserMapper;

    @Override
    public PageInfo<SmileOrganizationVo> organizationList(SmileOrganizationParam smileOrganizationParam) {
        PageInfo<SmileOrganizationVo> pageInfo;
        try {
            Map<String, Object> map = BeanUtil.convertBeanToMap(smileOrganizationParam);
            List<SmileOrganization> smileOrganizations = smileOrganizationMapper.selectOrganizationList(map);
            List<SmileOrganizationVo> smileOrganizationVos = smileOrganizations.stream().map(smileOrganization -> {
                SmileOrganizationVo smileOrganizationVo = new SmileOrganizationVo();
                BeanUtils.copyProperties(smileOrganization, smileOrganizationVo);
                log.info("organizationList orgId:{}", smileOrganization.getId());
                //关联查询人数
                smileOrganizationVo.setProper(smileUserMapper.strip(smileOrganization.getId()));
                return smileOrganizationVo;
            }).collect(Collectors.toList());
            pageInfo = new PageInfo<>(smileOrganizationVos);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return pageInfo;
    }

    @Override
    public AjaxResult create(SmileOrganizationVo smileOrganizationVo) {
        log.info("smileOrganizationVo:{}", smileOrganizationVo);
        LambdaQueryWrapper<SmileOrganization> queryWrapper = Wrappers.lambdaQuery(SmileOrganization.class);
        queryWrapper.eq(SmileOrganization::getName, smileOrganizationVo.getName());
        if (smileOrganizationMapper.selectOne(queryWrapper) == null) {
            SmileOrganization smileOrganization = new SmileOrganization();
            BeanUtils.copyProperties(smileOrganizationVo, smileOrganization);
            smileOrganization.setCreatedDate(LocalDateTime.now());
            smileOrganization.setUpdatedDate(LocalDateTime.now());
            smileOrganization.setDelTag(0);
            smileOrganizationMapper.insert(smileOrganization);
            return AjaxResult.success();
        }
        return AjaxResult.warn("总代理不能为空或组织已添加");
    }

    @Override
    public AjaxResult update(SmileOrganization smileOrganization) {
        if (StringUtils.isNotEmpty(smileOrganization.getAgentId())){
            smileOrganization.setUpdatedDate(LocalDateTime.now());
            smileOrganizationMapper.updateById(smileOrganization);
            return AjaxResult.success();
        }
       return AjaxResult.warn("总代理不能为空");
    }

    @Override
    public SmileOrgAndUserVo getOrgById(Long id) {
        SmileOrgAndUserVo smileOrgAndUserVo = new SmileOrgAndUserVo();
        //查询代理信息
        LambdaQueryWrapper<SmileOrganization> orgQueryWrapper = Wrappers.lambdaQuery(SmileOrganization.class);
        orgQueryWrapper.eq(SmileOrganization::getId, id);
        orgQueryWrapper.eq(SmileOrganization::getDelTag, 0);
        SmileOrganization smileOrganization = smileOrganizationMapper.selectOne(orgQueryWrapper);
        if (smileOrganization != null) {
            BeanUtils.copyProperties(smileOrganization, smileOrgAndUserVo);
            LambdaQueryWrapper<SmileUser> userQueryWrapper = Wrappers.lambdaQuery(SmileUser.class);
            userQueryWrapper.eq(SmileUser::getOrgId, id);
            userQueryWrapper.eq(SmileUser::getDelTag, 0);
            List<SmileUser> smileUsers = smileUserMapper.selectList(userQueryWrapper);
            List<SmileUserVoOrg> smileUserVoOrgList = new ArrayList<>();
            smileUsers.forEach(smileUser -> {
                SmileUserVoOrg smileUserVoOrg = new SmileUserVoOrg();
                BeanUtils.copyProperties(smileUser, smileUserVoOrg);
                //关联组织id筛选
                if (smileOrganization.getAgentId() != null && smileOrganization.getAgentId().equals(smileUser.getUid())) {
                    smileOrgAndUserVo.setSmileUserVoOrgListTotal(smileUserVoOrg);
                } else {
                    smileUserVoOrgList.add(smileUserVoOrg);
                }
            });
            smileOrgAndUserVo.setSmileUserVoOrgListOrdinary(smileUserVoOrgList);
            return smileOrgAndUserVo;
        }
        return null;
    }


}
