package ink.wgink.service.position.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.position.PositionDTO;
import ink.wgink.pojo.pos.PositionPO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.service.position.dao.IPositionDao;
import ink.wgink.service.position.pojo.vos.PositionVO;
import ink.wgink.service.position.service.IPositionService;
import ink.wgink.service.position.service.IPositionUserService;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: PositionServiceImpl
 * @Description: 职位
 * @Author: admin
 * @Date: 2019-04-11 22:41:20
 * @Version: 1.0
 **/
@Service
public class PositionServiceImpl extends DefaultBaseService implements IPositionService {

    @Autowired
    private IPositionDao positionDao;
    @Autowired
    private IPositionUserService positionUserService;

    @Override
    public void save(PositionVO positionVO) {
        String parentCode = null;
        Map<String, Object> params = HashMapUtil.beanToMap(positionVO);
        String positionParentId = params.get("positionParentId").toString();
        if (!ISystemConstant.TREE_ROOT_ID.equals(positionParentId)) {
            params.put("positionId", positionParentId);
            PositionDTO positionDTO = positionDao.get(params);
            parentCode = positionDTO.getPositionCode();
        }
        String positionCode = getCode(parentCode, positionParentId);
        params.put("positionCode", positionCode);
        params.put("positionId", UUIDUtil.getUUID());
        setSaveInfo(params);
        positionDao.save(params);
    }

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

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

    @Override
    public void update(String positionId, PositionVO positionVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(positionVO);
        setUpdateInfo(params);
        positionDao.update(params);
    }

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

    @Override
    public List<PositionDTO> listAll(Map<String, Object> params) {
        List<PositionDTO> positionDTOs = list(params);
        listSubPositions(positionDTOs, params);
        return positionDTOs;
    }

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

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

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

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

    @Override
    public List<PositionDTO> list(Map<String, Object> params) {
        List<PositionDTO> srcPositionDTOs = positionDao.list(params);
        List<PositionDTO> destPositionDTO = new ArrayList<>(Arrays.asList(new PositionDTO[srcPositionDTOs.size()]));
        Collections.copy(destPositionDTO, srcPositionDTOs);
        return destPositionDTO;
    }

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

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

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

    @Override
    public Integer count(Map<String, Object> params) {
        Integer count = positionDao.count(params);
        return count == null ? 0 : count;
    }

    @Override
    public PositionDTO getParentByPositionId(String positionId) {
        if (StringUtils.isBlank(positionId)) {
            return null;
        }
        Map<String, Object> params = getHashMap(2);
        params.put("positionId", positionId);
        return positionDao.getParent(params);
    }

    @Override
    public List<PositionDTO> listParentByPositionIds(List<String> positionIds) {
        if (positionIds == null || positionIds.isEmpty()) {
            return new ArrayList<>();
        }
        Map<String, Object> params = getHashMap(2);
        params.put("positionIds", positionIds);
        return positionDao.listParent(params);
    }

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

    /**
     * 递归查询子组
     *
     * @param positionDTOs
     * @param params
     */
    private void listSubPositions(List<PositionDTO> positionDTOs, Map<String, Object> params) {
        for (PositionDTO positionDTO : positionDTOs) {
            params.put("positionParentId", positionDTO.getPositionId());
            List<PositionDTO> subPositionDTOs = list(params);
            positionDTO.setSubPositions(subPositionDTOs);
            listSubPositions(subPositionDTOs, params);
        }
    }

    /**
     * 获取code
     *
     * @param parentCode
     * @param parentId
     * @return
     */
    private String getCode(String parentCode, String parentId) {
        PositionDTO positionDTO = positionDao.getLastByParentId(parentId);
        String code = positionDTO != null ? positionDTO.getPositionCode() : "0000";
        return super.getNewCode(code, parentCode);
    }

}
