package cn.com.tenlion.organizationmanagement.service.organization.impl;

import cn.com.tenlion.organizationmanagement.dao.organization.IOrganizationDao;
import cn.com.tenlion.organizationmanagement.service.organization.IOrganizationService;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;

import cn.com.tenlion.organizationmanagement.pojo.dtos.organization.OrganizationDTO;
import cn.com.tenlion.organizationmanagement.pojo.vos.organization.OrganizationVO;
import cn.com.tenlion.organizationmanagement.pojo.bos.organization.OrganizationBO;
import cn.com.tenlion.organizationmanagement.pojo.pos.organization.OrganizationPO;
import cn.com.tenlion.organizationmanagement.service.organization.OrganizationApi;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: OrganizationServiceImpl
 * @Description: 组织管理
 * @Author: CodeFactory
 * @Date: 2021-04-28 11:06:42
 * @Version: 3.0
 **/
@Primary
@Service
public class OrganizationServiceImpl extends DefaultBaseService implements IOrganizationService,OrganizationApi {

    @Autowired
    private IOrganizationDao organizationDao;

    @Override
    public void save(OrganizationVO organizationVO) {
        saveReturnId(organizationVO);
    }

    @Override
    public void save(String token, OrganizationVO organizationVO) {
        saveReturnId(token, organizationVO);
    }

    @Override
    public String saveReturnId(OrganizationVO organizationVO) {
        return saveReturnId(null, organizationVO);
    }

    @Override
    public String saveReturnId(String token, OrganizationVO organizationVO) {
        String organizationId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(organizationVO);
        params.put("organizationId", organizationId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        organizationDao.save(params);
        return organizationId;
    }

    @Override
    public void remove(List<String> ids) {
        remove(null, ids);
    }

    @Override
    public void remove(String token, List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("organizationIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        organizationDao.remove(params);
    }

    @Override
    public void delete(List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("organizationIds", ids);
        organizationDao.delete(params);
    }

    @Override
    public void update(String organizationId, OrganizationVO organizationVO) {
        update(null, organizationId, organizationVO);
    }

    @Override
    public void update(String token, String organizationId, OrganizationVO organizationVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(organizationVO);
        params.put("organizationId", organizationId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        organizationDao.update(params);
    }

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

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

    @Override
    public OrganizationBO getBO(Map<String, Object> params) {
        return organizationDao.getBO(params);
    }

    @Override
    public OrganizationBO getBO(String organizationId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("organizationId", organizationId);
        return getBO(params);
    }

    @Override
    public OrganizationPO getPO(Map<String, Object> params) {
        return organizationDao.getPO(params);
    }

    @Override
    public OrganizationPO getPO(String organizationId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("organizationId", organizationId);
        return getPO(params);
    }

    @Override
    public List<OrganizationDTO> list(Map<String, Object> params) {
        return organizationDao.list(params);
    }

    @Override
    public List<OrganizationBO> listBO(Map<String, Object> params) {
        return organizationDao.listBO(params);
    }

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

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

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

}