package com.xmomen.module.beforehandpackagerecord.service.impl;

import com.xmomen.module.beforehandpackagerecord.entity.BeforehandPackageRecord;
import com.xmomen.module.beforehandpackagerecord.entity.BeforehandPackageRecordExample;
import com.xmomen.module.beforehandpackagerecord.mapper.BeforehandPackageRecordMapperExt;
import com.xmomen.module.beforehandpackagerecord.model.BeforehandPackageRecordCreate;
import com.xmomen.module.beforehandpackagerecord.model.BeforehandPackageRecordQuery;
import com.xmomen.module.beforehandpackagerecord.model.BeforehandPackageRecordUpdate;
import com.xmomen.module.beforehandpackagerecord.model.BeforehandPackageRecordModel;
import com.xmomen.module.beforehandpackagerecord.service.BeforehandPackageRecordService;
import com.xmomen.framework.mybatis.dao.MybatisDao;
import com.xmomen.framework.mybatis.page.Page;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service
public class BeforehandPackageRecordServiceImpl implements BeforehandPackageRecordService {

    @Autowired
    MybatisDao mybatisDao;

    @Override
    @Transactional
    public BeforehandPackageRecordModel createBeforehandPackageRecord(BeforehandPackageRecordModel beforehandPackageRecordModel) {
        // 调用新增预包装记录表实体对象方法
        BeforehandPackageRecord beforehandPackageRecord = createBeforehandPackageRecord(beforehandPackageRecordModel.getEntity());
        // 如果新增成功，则返回预包装记录表领域对象
        if(beforehandPackageRecord != null){
            return getOneBeforehandPackageRecordModel(beforehandPackageRecord.getId());
        }
        // 否则返回null
        return null;
    }

    @Override
    @Transactional
    public BeforehandPackageRecord createBeforehandPackageRecord(BeforehandPackageRecord beforehandPackageRecord) {
        // 调用mybatisDao的insertByModel方法，新增预包装记录表实体对象
        return mybatisDao.insertByModel(beforehandPackageRecord);
    }

    @Override
    @Transactional
    public List<BeforehandPackageRecordModel> createBeforehandPackageRecords(List<BeforehandPackageRecordModel> beforehandPackageRecordModels) {
        // 定义预包装记录表领域对象集合
        List<BeforehandPackageRecordModel> beforehandPackageRecordModelList = null;
        // 遍历预包装记录表对象集合
        for (BeforehandPackageRecordModel beforehandPackageRecordModel : beforehandPackageRecordModels) {
            // 调用新增预包装记录表方法
            beforehandPackageRecordModel = createBeforehandPackageRecord(beforehandPackageRecordModel);
            // 如果新增成功，则将预包装记录表领域对象添加到集合中
            if(beforehandPackageRecordModel != null){
                if(beforehandPackageRecordModelList == null){
                    beforehandPackageRecordModelList = new ArrayList<>();
                }
                beforehandPackageRecordModelList.add(beforehandPackageRecordModel);
            }
        }
        // 返回预包装记录表领域对象集合
        return beforehandPackageRecordModelList;
    }


    @Override
    @Transactional
    public void updateBeforehandPackageRecord(BeforehandPackageRecordModel beforehandPackageRecordModel) {
        // 调用mybatisDao的update方法，更新预包装记录表
        mybatisDao.update(beforehandPackageRecordModel.getEntity());
    }

    @Override
    @Transactional
    public void updateBeforehandPackageRecord(BeforehandPackageRecord beforehandPackageRecord) {
        // 调用mybatisDao的update方法，更新预包装记录表实体对象
        mybatisDao.update(beforehandPackageRecord);
    }

    @Override
    @Transactional
    public void deleteBeforehandPackageRecord(String[] ids) {
        // 创建预包装记录表查询对象
        BeforehandPackageRecordExample beforehandPackageRecordExample = new BeforehandPackageRecordExample();
        // 设置查询条件
        beforehandPackageRecordExample.createCriteria().andIdIn(Arrays.<String>asList((String[]) ids));
        // 调用mybatisDao的deleteByExample方法，删除预包装记录表
        mybatisDao.deleteByExample(beforehandPackageRecordExample);
    }


    @Override
    @Transactional
    public void deleteBeforehandPackageRecord(String id) {
        // 调用mybatisDao的deleteByPrimaryKey方法，删除预包装记录表
        mybatisDao.deleteByPrimaryKey(BeforehandPackageRecord.class, id);
    }

    @Override
    public Page<BeforehandPackageRecordModel> getBeforehandPackageRecordModelPage(int limit, int offset, BeforehandPackageRecordQuery beforehandPackageRecordQuery) {
        // 调用mybatisDao的selectPage方法，查询预包装记录表领域分页对象
        return (Page<BeforehandPackageRecordModel>) mybatisDao.selectPage(BeforehandPackageRecordMapperExt.BeforehandPackageRecordMapperNameSpace + "getBeforehandPackageRecordModel", beforehandPackageRecordQuery, limit, offset);
    }


    @Override
    public Page<BeforehandPackageRecordModel> getBeforehandPackageRecordModelPage(int limit, int offset) {
        // 调用mybatisDao的selectPage方法，查询预包装记录表领域分页对象
        return (Page<BeforehandPackageRecordModel>) mybatisDao.selectPage(BeforehandPackageRecordMapperExt.BeforehandPackageRecordMapperNameSpace + "getBeforehandPackageRecordModel", null, limit, offset);
    }

    @Override
    public List<BeforehandPackageRecordModel> getBeforehandPackageRecordModelList(BeforehandPackageRecordQuery beforehandPackageRecordQuery) {
        // 调用mybatisDao的getSqlSessionTemplate的selectList方法，查询预包装记录表领域集合对象
        return mybatisDao.getSqlSessionTemplate().selectList(BeforehandPackageRecordMapperExt.BeforehandPackageRecordMapperNameSpace + "getBeforehandPackageRecordModel", beforehandPackageRecordQuery);
    }

    @Override
    public List<BeforehandPackageRecordModel> getBeforehandPackageRecordModelList() {
        // 调用mybatisDao的getSqlSessionTemplate的selectList方法，查询预包装记录表领域集合对象
        return mybatisDao.getSqlSessionTemplate().selectList(BeforehandPackageRecordMapperExt.BeforehandPackageRecordMapperNameSpace + "getBeforehandPackageRecordModel");
    }

    @Override
    public BeforehandPackageRecord getOneBeforehandPackageRecord(String id) {
        // 调用mybatisDao的selectByPrimaryKey方法，查询预包装记录表实体对象
        return mybatisDao.selectByPrimaryKey(BeforehandPackageRecord.class, id);
    }

    @Override
    public BeforehandPackageRecordModel getOneBeforehandPackageRecordModel(String id) {
        // 创建预包装记录表查询对象
        BeforehandPackageRecordQuery beforehandPackageRecordQuery = new BeforehandPackageRecordQuery();
        // 设置查询条件
        beforehandPackageRecordQuery.setId(id);
        // 调用mybatisDao的getSqlSessionTemplate的selectOne方法，根据主键查询单个对象
        return mybatisDao.getSqlSessionTemplate().selectOne(BeforehandPackageRecordMapperExt.BeforehandPackageRecordMapperNameSpace + "getBeforehandPackageRecordModel", beforehandPackageRecordQuery);
    }

    @Override
    public BeforehandPackageRecordModel getOneBeforehandPackageRecordModel(BeforehandPackageRecordQuery beforehandPackageRecordQuery) throws TooManyResultsException {
        // 调用mybatisDao的getSqlSessionTemplate的selectOne方法，根据查询参数查询单个对象
        return mybatisDao.getSqlSessionTemplate().selectOne(BeforehandPackageRecordMapperExt.BeforehandPackageRecordMapperNameSpace + "getBeforehandPackageRecordModel", beforehandPackageRecordQuery);
    }
}
