package cn.com.tenlion.institutionmanagement.service.institution.impl;

import cn.com.tenlion.institutionmanagement.dao.institution.IInstitutionDao;
import cn.com.tenlion.institutionmanagement.pojo.bos.institution.InstitutionBO;
import cn.com.tenlion.institutionmanagement.pojo.dtos.institution.InstitutionDTO;
import cn.com.tenlion.institutionmanagement.pojo.pos.institution.InstitutionPO;
import cn.com.tenlion.institutionmanagement.pojo.vos.institution.InstitutionVO;
import cn.com.tenlion.institutionmanagement.service.institution.IInstitutionService;
import cn.com.tenlion.institutionmanagement.service.institution.InstitutionApi;
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 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.stereotype.Service;

import java.util.*;

/**
 * @ClassName: InstitutionServiceImpl
 * @Description: 机构管理
 * @Author: CodeFactory
 * @Date: 2021-04-28 17:58:49
 * @Version: 3.0
 **/
@Service
public class InstitutionServiceImpl extends DefaultBaseService implements IInstitutionService,InstitutionApi {

    @Autowired
    private IInstitutionDao institutionDao;

    @Override
    public void save(InstitutionVO institutionVO) {
        saveReturnId(institutionVO);
    }

    @Override
    public void save(String token, InstitutionVO institutionVO) {
        saveReturnId(token, institutionVO);
    }

    @Override
    public String saveReturnId(InstitutionVO institutionVO) {
        return saveReturnId(null, institutionVO);
    }

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

    @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("institutionIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        institutionDao.remove(params);
    }

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

    @Override
    public void update(String institutionId, InstitutionVO institutionVO) {
        update(null, institutionId, institutionVO);
    }

    @Override
    public void update(String token, String institutionId, InstitutionVO institutionVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(institutionVO);
        params.put("institutionId", institutionId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        institutionDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

}