package ink.wgink.service.group.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.pojo.dtos.group.GroupDTO;
import ink.wgink.pojo.pos.GroupPO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.service.group.dao.IGroupDao;
import ink.wgink.service.group.pojo.vos.GroupVO;
import ink.wgink.service.group.service.IGroupService;
import ink.wgink.service.group.service.IGroupUserService;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: GroupServiceImpl
 * @Description: 组
 * @Author: wenc
 * @Date: 2019/1/6 3:21 PM
 * @Version: 1.0
 **/
@Service
public class GroupServiceImpl extends DefaultBaseService implements IGroupService {

    @Autowired
    private IGroupDao groupDao;
    @Autowired
    private IGroupUserService groupUserService;

    @Override
    public void save(GroupVO groupVO) {
        String parentCode = null;
        Map<String, Object> params = HashMapUtil.beanToMap(groupVO);
        String groupParentId = params.get("groupParentId").toString();
        if (!ISystemConstant.TREE_ROOT_ID.equals(groupParentId)) {
            params.put("groupId", groupParentId);
            GroupDTO groupDTO = groupDao.get(params);
            parentCode = groupDTO.getGroupCode();
        }
        String groupCode = getCode(parentCode, groupParentId);
        params.put("groupCode", groupCode);
        params.put("groupId", UUIDUtil.getUUID());
        setSaveInfo(params);
        groupDao.save(params);
    }

    @Override
    public void remove(List<String> ids) {
        // 删除用户
        groupUserService.delete(ids);

        Map<String, Object> params = getHashMap(2);
        params.put("groupIds", ids);
        setUpdateInfo(params);
        groupDao.remove(params);
    }

    @Override
    public void update(String groupId, GroupVO groupVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(groupVO);
        setUpdateInfo(params);
        groupDao.update(params);
    }

    @Override
    public List<GroupDTO> listAllByParentId(String groupParentId) {
        Map<String, Object> params = getHashMap(2);
        params.put("groupParentId", groupParentId);
        return listAll(params);
    }

    @Override
    public List<GroupDTO> listAll(Map<String, Object> params) {
        List<GroupDTO> groupDTOs = list(params);
        listSubGroups(groupDTOs, params);
        return groupDTOs;
    }

    @Override
    public GroupDTO get(Map<String, Object> params) {
        return groupDao.get(params);
    }

    @Override
    public GroupDTO get(String groupId) {
        Map<String, Object> params = getHashMap(2);
        params.put("groupId", groupId);
        return get(params);
    }

    @Override
    public List<ZTreeDTO> listZTree(Map<String, Object> params) {
        List<ZTreeDTO> zTreeDTOs = groupDao.listZTree(params);
        for (ZTreeDTO zTreeDTO : zTreeDTOs) {
            Integer subCount = groupDao.countByParentId(zTreeDTO.getId());
            setZTreeInfo(zTreeDTO, subCount);
        }
        return zTreeDTOs;
    }

    @Override
    public SuccessResultList<List<GroupDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<GroupDTO> groupDTOs = groupDao.list(page.getParams());
        PageInfo<GroupDTO> pageInfo = new PageInfo<>(groupDTOs);
        return new SuccessResultList<>(groupDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public List<GroupDTO> list(Map<String, Object> params) {
        List<GroupDTO> srcGroupDTOs = groupDao.list(params);
        List<GroupDTO> destGroupDTOs = new ArrayList<>(Arrays.asList(new GroupDTO[srcGroupDTOs.size()]));
        Collections.copy(destGroupDTOs, srcGroupDTOs);
        return destGroupDTOs;
    }

    @Override
    public List<GroupDTO> list(List<String> groupIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("groupIds", groupIds);
        return list(params);
    }

    @Override
    public List<GroupPO> listPO(Map<String, Object> params) {
        return groupDao.listPO(params);
    }

    @Override
    public List<GroupPO> listPO(List<String> groupIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("groupIds", groupIds);
        return listPO(params);
    }

    /**
     * 递归查询子组
     *
     * @param groupDTOs
     * @param params
     */
    private void listSubGroups(List<GroupDTO> groupDTOs, Map<String, Object> params) {
        for (GroupDTO groupDTO : groupDTOs) {
            params.put("groupParentId", groupDTO.getGroupId());
            List<GroupDTO> subGroupDTOs = list(params);
            groupDTO.setSubGroups(subGroupDTOs);
            listSubGroups(subGroupDTOs, params);
        }
    }

    /**
     * 获取code
     *
     * @param parentCode
     * @param parentId
     * @return
     */
    private String getCode(String parentCode, String parentId) {
        GroupDTO groupDTO = groupDao.getLastByParentId(parentId);
        String code = groupDTO != null ? groupDTO.getGroupCode() : "0000";
        return super.getNewCode(code, parentCode);
    }

}
