package ink.wgink.database.service.mysql.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.database.dao.mysql.IMysqlDao;
import ink.wgink.database.pojo.bos.mysql.MysqlBO;
import ink.wgink.database.pojo.dtos.mysql.MysqlDTO;
import ink.wgink.database.pojo.pos.mysql.MysqlPO;
import ink.wgink.database.pojo.vos.mysql.MysqlVO;
import ink.wgink.database.service.mysql.IMysqlService;
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: MysqlServiceImpl
 * @Description: mysql连接
 * @Author: CodeFactory
 * @Date: 2021-10-26 23:05:32
 * @Version: 3.0
 **/
@Service
public class MysqlServiceImpl extends DefaultBaseService implements IMysqlService {

    @Autowired
    private IMysqlDao mysqlDao;

    @Override
    public void save(MysqlVO mysqlVO) {
        saveReturnId(mysqlVO);
    }

    @Override
    public void save(String token, MysqlVO mysqlVO) {
        saveReturnId(token, mysqlVO);
    }

    @Override
    public String saveReturnId(MysqlVO mysqlVO) {
        return saveReturnId(null, mysqlVO);
    }

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

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

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

    @Override
    public void update(String mysqlId, MysqlVO mysqlVO) {
        update(null, mysqlId, mysqlVO);
    }

    @Override
    public void update(String token, String mysqlId, MysqlVO mysqlVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(mysqlVO);
        params.put("mysqlId", mysqlId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        mysqlDao.update(params);
    }

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

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

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

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

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

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

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

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

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

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

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

}