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

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.module.map.dao.userlocation.IUserLocationDao;
import ink.wgink.module.map.pojo.bos.userlocation.UserLocationBO;
import ink.wgink.module.map.pojo.dtos.userlocation.UserLocationDTO;
import ink.wgink.module.map.pojo.pos.userlocation.UserLocationPO;
import ink.wgink.module.map.pojo.vos.userlocation.UserLocationVO;
import ink.wgink.module.map.service.userlocation.IUserLocationService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
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.List;
import java.util.Map;

/**
 * @ClassName: UserLocationServiceImpl
 * @Description: 用户定位
 * @Author: CodeFactory
 * @Date: 2021-10-29 15:37:01
 * @Version: 3.0
 **/
@Service
public class UserLocationServiceImpl extends DefaultBaseService implements IUserLocationService {

    @Autowired
    private IUserLocationDao userLocationDao;

    @Override
    public void save(String userId, UserLocationVO userLocationVO) {
        saveAndReturnId(userId, userLocationVO);
    }

    @Override
    public String saveAndReturnId(String userId, UserLocationVO userLocationVO) {
        String userLocationId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(userLocationVO);
        params.put("userLocationId", userLocationId);
        params.put("isDelete", 0);
        userLocationDao.save(params);
        return userLocationId;
    }

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

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

    @Override
    public void update(String userLocationId, UserLocationVO userLocationVO) {
        update(null, userLocationId, userLocationVO);
    }

    @Override
    public void update(String token, String userLocationId, UserLocationVO userLocationVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(userLocationVO);
        params.put("userLocationId", userLocationId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        userLocationDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

}