package com.yonge.cooleshow.biz.dal.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yonge.cooleshow.biz.dal.entity.SysArea;
import com.yonge.cooleshow.biz.dal.entity.SysUser;
import com.yonge.cooleshow.biz.dal.mapper.SysUserMapper;
import com.yonge.cooleshow.biz.dal.service.SysAreaService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import com.yonge.cooleshow.biz.dal.entity.UnbindAuthUser;
import com.yonge.cooleshow.biz.dal.wrapper.UnbindAuthUserWrapper;
import com.yonge.cooleshow.biz.dal.mapper.UnbindAuthUserMapper;
import com.yonge.cooleshow.biz.dal.service.UnbindAuthUserService;

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

/**
 * 解绑审核人员设置
 * 2023-08-23 16:33:16
 */
@Slf4j
@Service
public class UnbindAuthUserServiceImpl extends ServiceImpl<UnbindAuthUserMapper, UnbindAuthUser> implements UnbindAuthUserService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysAreaService sysAreaService;

    /**
     * 查询详情
     * @param id 详情ID
     * @return UnbindAuthUser
     */
    @Override
    public UnbindAuthUser detail(Long id) {
        
        return baseMapper.selectById(id);
    }
    
    /**
     * 分页查询
     * @param page IPage<UnbindAuthUser>
     * @param query UnbindAuthUserWrapper.UnbindAuthUserQuery
     * @return IPage<UnbindAuthUser>
     */
    @Override
    public IPage<UnbindAuthUserWrapper.UnbindAuthUser> selectPage(IPage<UnbindAuthUserWrapper.UnbindAuthUser> page, UnbindAuthUserWrapper.UnbindAuthUserQuery query) {

        IPage<UnbindAuthUserWrapper.UnbindAuthUser> authUserIPage = page.setRecords(baseMapper.selectPage(page, query));
        List<UnbindAuthUserWrapper.UnbindAuthUser> records = authUserIPage.getRecords();

        if (CollectionUtils.isEmpty(records)) {
            return authUserIPage;
        }

        // 设置员工信息
        List<Long> userIds = records.stream().map(UnbindAuthUserWrapper.UnbindAuthUser::getUserId).collect(Collectors.toList());
        Map<Long, SysUser> userMap = sysUserService.getMapByIds(userIds);

        List<Integer> codes = records.stream()
                .flatMap(o -> Lists.newArrayList(o.getProvinceCode(), o.getCityCode(), o.getRegionCode()).stream())
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        Map<Integer, SysArea> areaMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(codes)) {
          List<SysArea> sysAreas = sysAreaService.queryByCodes(codes);
          if (CollectionUtils.isNotEmpty(sysAreas)) {
              areaMap = sysAreas.stream().collect(Collectors.toMap(SysArea::getCode, o -> o));
          }
        }

        for (UnbindAuthUserWrapper.UnbindAuthUser record : records) {
            SysUser sysUser = userMap.get(record.getUserId());
            if (sysUser != null) {
                record.setUserName(sysUser.getUsername());
                record.setUserPhone(sysUser.getPhone());
            }

            SysArea province = areaMap.get(record.getProvinceCode());
            if (province != null) {
                record.setProvinceName(province.getName());
            }

            SysArea city = areaMap.get(record.getCityCode());
            if (city != null) {
                record.setCityName(city.getName());
            }

            SysArea region = areaMap.get(record.getRegionCode());
            if (region != null) {
                record.setRegionName(region.getName());
            }
        }
        return authUserIPage;
    }

    /**
     * 添加
     * @param unbindAuthUser UnbindAuthUserWrapper.UnbindAuthUser
     * @return Boolean
     */
    @Override
    public Boolean add(UnbindAuthUserWrapper.UnbindAuthUser unbindAuthUser) {

        return this.save(JSON.parseObject(unbindAuthUser.jsonString(), UnbindAuthUser.class));
    }

    /**
     * 更新
     * @param unbindAuthUser UnbindAuthUserWrapper.UnbindAuthUser
     * @return Boolean
     */
    @Override
    public Boolean update(UnbindAuthUserWrapper.UnbindAuthUser unbindAuthUser){

        return this.updateById(JSON.parseObject(unbindAuthUser.jsonString(), UnbindAuthUser.class));       
    }
}