package ink.wgink.module.map.service.grid.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.SearchException;
import ink.wgink.module.map.dao.grid.IGridGroupDao;
import ink.wgink.module.map.pojo.dtos.grid.GridGroupDTO;
import ink.wgink.module.map.pojo.pos.grid.GridGroupPO;
import ink.wgink.module.map.pojo.vos.grid.GridGroupVO;
import ink.wgink.module.map.service.grid.IGridGroupService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
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.List;
import java.util.Map;

/**
 * @ClassName: GridGroupServiceImpl
 * @Description: 网格组业务
 * @Author: wanggeng
 * @Date: 2021/10/19 10:25 下午
 * @Version: 1.0
 */
@Service
public class GridGroupServiceImpl extends DefaultBaseService implements IGridGroupService {

    @Autowired
    private IGridGroupDao gridGroupDao;

    @Override
    public void save(GridGroupVO gridGroupVO) throws Exception {
        saveAndReturnId(gridGroupVO);
    }

    @Override
    public String saveAndReturnId(GridGroupVO gridGroupVO) throws Exception {
        GridGroupPO gridGroupPO = getPOByCode(gridGroupVO.getGridGroupCode());
        if (gridGroupPO != null) {
            throw new SearchException("网格组编码存在");
        }
        String gridGroupId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(gridGroupVO);
        params.put("gridGroupId", gridGroupId);
        setSaveInfo(params);
        gridGroupDao.save(params);
        return gridGroupId;
    }

    @Override
    public void remove(List<String> ids) {
        if (ids.isEmpty()) {
            return;
        }
        Map<String, Object> params = getHashMap(2);
        params.put("gridGroupIds", ids);
        setUpdateInfo(params);
        gridGroupDao.remove(params);
    }

    @Override
    public void update(String gridGroupId, GridGroupVO gridGroupVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(gridGroupVO);
        params.put("gridGroupId", gridGroupId);
        setUpdateInfo(params);
        gridGroupDao.update(params);
    }

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

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

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

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

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

    @Override
    public GridGroupPO getPOByCode(String gridGroupCode) {
        Map<String, Object> params = getHashMap(2);
        params.put("gridGroupCode", gridGroupCode);
        return getPO(params);
    }

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

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