/*
 * ShouGouZiGeDomain.java
 * 版权所有 © 2011-2017 北京神舟良实信息技术有限公司
 * Copyright © 2011-2017 All Rights Reserved    京ICP备13013562号
 *  created by lipenghe with LAPTOP-DPSC399A
 */
package com.unis.base.server.domain;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.unis.base.common.util.IdWorkerUtils;
import com.unis.base.common.vo.ShouGouZiGeVO;
import com.unis.base.server.dao.ShouGouZiGeDao;
import com.unis.base.server.entity.ShouGouZiGeDO;
import com.unis.common.utils.JsonUtils;
import com.unis.common.utils.LsBeanUtils;
import com.unis.common.utils.ResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * TY_SHOUGOUZIGE-真收购资格表
 * DO处理自身, VO处理包括子表
 * @author lipenghe at 
 */
@Component
//Transactional注解加在service
//@Transactional
@Slf4j
public class ShouGouZiGeDomain {

    @Autowired
    ShouGouZiGeDao shouGouZiGeDao;

    @Autowired
    IdWorkerUtils idWorkerUtils;

    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    /**
     * 通用的数据处理,只对VO参数接口有效,早于buildInsertData, buildUpdateData
     * @param ziGeVO    <真收购资格表>对象
     */
    private void buildData(ShouGouZiGeVO ziGeVO){
        //ShouGouZiGeVO ziGeVODb= selectById(ziGeVO);
    }

    /**
     * 通用的插入处理,通用最晚执行
     * @param entity    <真收购资格表>对象
     */
    private void buildInsertData(ShouGouZiGeDO entity){
        Long pk= null;
        //当前为主表
        pk= idWorkerUtils.nextId();
        entity.setSgzgid(pk);
        entity.buildInsertData(String.valueOf(pk));
        entity.setVersion(0);
    }

    /**
     * 通用的插入处理,只对VO参数接口有效,晚于buildData
     * @param ziGeVO    <真收购资格表>对象
     */
    private void buildInsertData(ShouGouZiGeVO ziGeVO){

    }

    /**
     * 通用的插入处理,只对VO参数接口有效,晚于buildInsertData
     * 专用于处理 VO 部分字段因安全等原因不能序列化为字符串的情况
     * @param ziGeVO    <真收购资格表>对象
     */
    private void buildInsertVoToDoData(ShouGouZiGeVO ziGeVO, ShouGouZiGeDO entity){

    }

    /**
     * 通用的更新处理,通用最晚执行
     * @param entity    <真收购资格表>对象
     */
    private void buildUpdateData(ShouGouZiGeDO entity){
        entity.buildUpdateData();
    }

    /**
     * 新增成功后,只对VO参数接口有效
     * @param ziGeVO    <真收购资格表>对象
     */
    private void insertSuccess(ShouGouZiGeVO ziGeVO){

    }

    /**
     * 修改成功后,只对VO参数接口有效
     * @param ziGeVO    <真收购资格表>对象
     */
    private void updateSuccess(ShouGouZiGeVO ziGeVO){

    }

    /**
     * 新增和修改成功后,只对VO参数接口有效,早于insertSuccess, updateSuccess
     * @param ziGeVO    <真收购资格表>对象
     */
    private void saveSuccess(ShouGouZiGeVO ziGeVO){

    }

    /**
     * 通用的返回数据处理,只对VO参数接口有效
     * @param ziGeVO    <真收购资格表>对象
     */
    private ShouGouZiGeVO buildResult(ShouGouZiGeVO ziGeVO){
        ShouGouZiGeVO ziGeVOResult= new ShouGouZiGeVO();
        ziGeVOResult.setSgzgid(ziGeVO.getSgzgid());
        return ziGeVOResult;
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>非主键的业务唯一属性校验
     * @param ziGeVO    <真收购资格表>对象
     * @param check    是否进行非主键的唯一校验
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper uniqueCheck(ShouGouZiGeVO ziGeVO, boolean check) {
        if(check){
            ShouGouZiGeVO shouGouZiGeVO=new ShouGouZiGeVO();
            shouGouZiGeVO.setTyshxydm(ziGeVO.getTyshxydm());
            shouGouZiGeVO.setYyzzzch(ziGeVO.getYyzzzch());
            shouGouZiGeVO.setYxzt(1);
            int num= shouGouZiGeDao.selectCountByEntity(shouGouZiGeVO);
            if(num>0){
                return ResponseWrapper.fail("现行有效的收购资格只能有一个");
            }else{
                return ResponseWrapper.success();
            }
        }else{
            return ResponseWrapper.success();
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>插入校验
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper insertCheck(ShouGouZiGeVO ziGeVO) {
        //数据校验
        boolean enable= true;
        if(enable){
            return ResponseWrapper.success();
        }else{
            return ResponseWrapper.fail("不允许新增");
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>修改校验
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper updateCheck(ShouGouZiGeVO ziGeVO) {
        //数据校验
        boolean enable= true;
        /*List<ShouGouZiGeVO> ziGeVOList= shouGouZiGeDao.selectByEntity(ziGeVO);
        for(ShouGouZiGeVO temp : ziGeVOList){
            if(1== 1){
                //..................
                break;
            }
        }*/
        if(enable){
            return ResponseWrapper.success();
        }else{
            return ResponseWrapper.fail("存在不可更新的数据");
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>删除校验
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper deleteCheck(ShouGouZiGeVO ziGeVO) {
        //数据校验
        boolean enable= true;
        /*List<ShouGouZiGeVO> ziGeVOList= shouGouZiGeDao.selectByEntity(ziGeVO);
        for(ShouGouZiGeVO temp : ziGeVOList){
            if(1== 1){
                //..................
                break;
            }
        }*/
        if(enable){
            return ResponseWrapper.success();
        }else{
            return ResponseWrapper.fail("存在不可删除的数据或无权限操作");
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>选择性保存,包含新增和修改
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper saveEntitySelective(ShouGouZiGeVO ziGeVO) {
        if((ziGeVO.getSgzgid() != null)){
            return updateByEntitySelective(ziGeVO);
        }else{
            return insertEntitySelective(ziGeVO, true);
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>插入全部字段
     * @param entity    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int insertEntity(ShouGouZiGeDO entity) {
        buildInsertData(entity);
        return shouGouZiGeDao.insert(entity);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>插入全部字段
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper insertEntity(ShouGouZiGeVO ziGeVO) {
        ResponseWrapper responseWrapperCheck = insertCheck(ziGeVO);
        if(responseWrapperCheck.isSuccess()){
            ResponseWrapper responseWrapper= uniqueCheck(ziGeVO, true);
            if(responseWrapper.isSuccess()){
                buildData(ziGeVO);
                buildInsertData(ziGeVO);

                //有属性操作则优先在vo处理
                ShouGouZiGeDO entity= LsBeanUtils.copyProperties(ziGeVO, ShouGouZiGeDO.class);
                //处理不能序列化的字段
                buildInsertVoToDoData(ziGeVO, entity);
                int num= insertEntity(entity);
                if(num> 0){
                    //新增成功后
                    saveSuccess(ziGeVO);
                    insertSuccess(ziGeVO);

                    //主键设置回去
                    ziGeVO.setSgzgid(entity.getSgzgid());
                    return ResponseWrapper.success(buildResult(ziGeVO));
                }
                return ResponseWrapper.fail();
            }else{
                return responseWrapper;
            }
        }else{
            return responseWrapperCheck;
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>选择性插入字段
     * @param entity    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int insertEntitySelective(ShouGouZiGeDO entity) {
        buildInsertData(entity);
        return shouGouZiGeDao.insertSelective(entity);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>选择性插入字段
     * @param ziGeVO    <真收购资格表>对象
     * @param check    是否进行非主键的唯一校验
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper insertEntitySelective(ShouGouZiGeVO ziGeVO, boolean check) {
        ResponseWrapper responseWrapperCheck = insertCheck(ziGeVO);
        if(responseWrapperCheck.isSuccess()){
            ResponseWrapper responseWrapper= uniqueCheck(ziGeVO, check);
            if(responseWrapper.isSuccess()){
                buildData(ziGeVO);
                buildInsertData(ziGeVO);

                //有属性操作则优先在vo处理
                ShouGouZiGeDO entity= LsBeanUtils.copyProperties(ziGeVO, ShouGouZiGeDO.class);
                //处理不能序列化的字段
                buildInsertVoToDoData(ziGeVO, entity);
                int num= insertEntitySelective(entity);
                if(num> 0){
                    //新增成功后
                    saveSuccess(ziGeVO);
                    insertSuccess(ziGeVO);

                    //主键设置回去
                    ziGeVO.setSgzgid(entity.getSgzgid());
                    return ResponseWrapper.success(buildResult(ziGeVO));
                }
                return ResponseWrapper.fail();
            }else{
                return responseWrapper;
            }
        }else{
            return responseWrapperCheck;
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>批量插入全部字段
     * @param ziGeVOList    <真收购资格表>对象列表
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int insertBatchEntityList(List<ShouGouZiGeVO> ziGeVOList) {
        int eachNum= 500;

        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);

        ShouGouZiGeVO ziGeVO= null;
        for(int i=0, size= ziGeVOList.size(); i< size; i++){
            ziGeVO= ziGeVOList.get(i);
            insertEntitySelective(ziGeVO, false);
            if(i % eachNum == 0 || i == size-1){
                //手动每eachNum条提交一次，提交后无法回滚
                sqlSession.commit();
                //清理缓存，防止溢出
                sqlSession.clearCache();
            }
        }
        return ziGeVOList.size();

        /*
        for(ShouGouZiGeVO ziGeVOTemp : ziGeVOList){
            buildData(ziGeVOTemp);
            buildInsertData(ziGeVOTemp);
        }
        List<ShouGouZiGeDO> ziGeDOList= JsonUtils.parseList(
            JsonUtils.serialize(ziGeVOList), ShouGouZiGeDO.class);
        int num= 0;
        int max= ziGeDOList.size();
        int step= Math.min(eachNum, max);
        for(int i= 0; i< max; i+= step){
            List<ShouGouZiGeDO> listEach= ziGeDOList.subList(i, Math.min(i+step, max));
            num+= shouGouZiGeDao.insertBatchEntityList(listEach);
        }
        return num;*/
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>更改状态
     * @param entity    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int updateStatusByEntity(ShouGouZiGeDO entity) {
        buildUpdateData(entity);
        return shouGouZiGeDao.updateStatusByEntity(entity);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>更改状态
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper updateStatusByEntity(ShouGouZiGeVO ziGeVO) {
        ResponseWrapper responseWrapperCheck = updateCheck(ziGeVO);
        if(responseWrapperCheck.isSuccess()){
            ResponseWrapper responseWrapper= uniqueCheck(ziGeVO, true);
            if(responseWrapper.isSuccess()){
                buildData(ziGeVO);
                
                //有属性操作则优先在vo处理
                ShouGouZiGeDO entity= JsonUtils.parse(
                    JsonUtils.serialize(ziGeVO), ShouGouZiGeDO.class);
                int num= updateStatusByEntity(entity);
                if(num> 0){
                    //修改成功后
                    saveSuccess(ziGeVO);
                    updateSuccess(ziGeVO);

                    //主键设置回去(此处实际不需要)
                    ziGeVO.setSgzgid(entity.getSgzgid());
                    return ResponseWrapper.success(buildResult(ziGeVO));
                }else{
                    return ResponseWrapper.fail();
                }
            }else{
                return responseWrapper;
            }
        }else{
            return responseWrapperCheck;
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体批量更新全部字段
     * @param list    <真收购资格表>对象列表
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int updateBatchByEntityList(List<ShouGouZiGeDO> list) {
        for(ShouGouZiGeDO temp : list){
            buildUpdateData(temp);
        }
        return shouGouZiGeDao.updateBatchByEntityList(list);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体某些属性更新全部字段
     * @param entity    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int updateByEntity(ShouGouZiGeDO entity) {
        buildUpdateData(entity);
        return shouGouZiGeDao.updateByEntity(entity);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体某些属性更新全部字段
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper updateByEntity(ShouGouZiGeVO ziGeVO) {
        ResponseWrapper responseWrapperCheck = updateCheck(ziGeVO);
        if(responseWrapperCheck.isSuccess()){
            ResponseWrapper responseWrapper= uniqueCheck(ziGeVO, true);
            if(responseWrapper.isSuccess()){
                buildData(ziGeVO);
                //有属性操作则优先在vo处理
                ShouGouZiGeDO entity= JsonUtils.parse(
                    JsonUtils.serialize(ziGeVO), ShouGouZiGeDO.class);
                int num= updateByEntity(entity);
                if(num> 0){
                    //修改成功后
                    saveSuccess(ziGeVO);
                    updateSuccess(ziGeVO);

                    //主键设置回去(此处实际不需要)
                    ziGeVO.setSgzgid(entity.getSgzgid());
                    return ResponseWrapper.success(buildResult(ziGeVO));
                }
                return ResponseWrapper.fail();
            }else{
                return responseWrapper;
            }
        }else{
            return responseWrapperCheck;
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体不为空的属性,选择性更新字段
     * @param entity    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int updateByEntitySelective(ShouGouZiGeDO entity) {
        buildUpdateData(entity);
        return shouGouZiGeDao.updateByEntitySelective(entity);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体不为空的属性,选择性更新字段(存在则更新,不存在则插入,其余删除)
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper updateByEntitySelective(ShouGouZiGeVO ziGeVO) {
        ResponseWrapper responseWrapperCheck = updateCheck(ziGeVO);
        if(responseWrapperCheck.isSuccess()){
            ResponseWrapper responseWrapper= uniqueCheck(ziGeVO, true);
            if(responseWrapper.isSuccess()){
                buildData(ziGeVO);
                //此处是直接更新,较为繁琐,适用于子表数据有关联(被其他数据使用),缺少排序在字段会出现顺序与修改数据的顺序不一致的问题,
                //有属性操作则优先在vo处理
                ShouGouZiGeDO entity= JsonUtils.parse(
                    JsonUtils.serialize(ziGeVO), ShouGouZiGeDO.class);
                int num= updateByEntitySelective(entity);
                if(num> 0){
                    //修改成功后
                    saveSuccess(ziGeVO);
                    updateSuccess(ziGeVO);

                    //主键设置回去(此处实际不需要)
                    ziGeVO.setSgzgid(entity.getSgzgid());
                    return ResponseWrapper.success(buildResult(ziGeVO));
                }
                return ResponseWrapper.fail();
            }else{
                return responseWrapper;
            }
        }else{
            return responseWrapperCheck;
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>[真删除]根据实体的某些属性进行单条或者批量删除
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper deleteByEntity(ShouGouZiGeVO ziGeVO) {
        ResponseWrapper responseWrapper = deleteCheck(ziGeVO);
        if(responseWrapper.isSuccess()){
            int num= shouGouZiGeDao.deleteByEntity(ziGeVO);
            return ResponseWrapper.choose(num);
        }else{
            return responseWrapper;
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>[真删除]根据实体的某些属性进行单条或者批量删除
     * @param entity    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int deleteByEntity(ShouGouZiGeDO entity) {
        ShouGouZiGeVO ziGeVO= JsonUtils.parse(
            JsonUtils.serialize(entity), ShouGouZiGeVO.class);
        return shouGouZiGeDao.deleteByEntity(ziGeVO);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>[假删除]根据实体的某些属性进行单条或者批量删除,根据实体属性单条或批量删除只是改变了状态不在页面显示,数据仍存在
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ResponseWrapper deleteShamByEntity(ShouGouZiGeVO ziGeVO) {
        ResponseWrapper responseWrapper = deleteCheck(ziGeVO);
        if(responseWrapper.isSuccess()){
            //自行在controller设置状态,则可删除,亦可还原
            int num= shouGouZiGeDao.deleteShamByEntity(ziGeVO);
            return ResponseWrapper.choose(num);
        }else{
            return responseWrapper;
        }
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>[假删除]根据实体的某些属性进行单条或者批量删除,根据实体属性单条或批量删除只是改变了状态不在页面显示,数据仍存在
     * @param entity    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int deleteShamByEntity(ShouGouZiGeDO entity) {
        ShouGouZiGeVO ziGeVO= JsonUtils.parse(
            JsonUtils.serialize(entity), ShouGouZiGeVO.class);
        return shouGouZiGeDao.deleteShamByEntity(ziGeVO);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体的某些属性查询数量
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int selectCountByEntity(ShouGouZiGeVO ziGeVO) {
        int num= shouGouZiGeDao.selectCountByEntity(ziGeVO);
        return num;
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体的唯一属性查询数量
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int selectCountById(ShouGouZiGeVO ziGeVO) {
        int num= shouGouZiGeDao.selectCountById(ziGeVO);
        return num;
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体的业务唯一属性(且非指定主键数据)判断是否存在
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public int selectExistsByEntity(ShouGouZiGeVO ziGeVO) {
        int num= shouGouZiGeDao.selectExistsByEntity(ziGeVO);
        return num;
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体(唯一标示)查询一条数据
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ShouGouZiGeVO selectById(ShouGouZiGeVO ziGeVO) {
        ShouGouZiGeVO ziGeVODb= shouGouZiGeDao.selectById(ziGeVO);
        return ziGeVODb;
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体(唯一标示)查询一条数据
     * @param entity    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public ShouGouZiGeVO selectById(ShouGouZiGeDO entity) {
        ShouGouZiGeVO ziGeVO= JsonUtils.parse(
            JsonUtils.serialize(entity), ShouGouZiGeVO.class);
        return shouGouZiGeDao.selectById(ziGeVO);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体的某些属性查询分页数据
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public Page<ShouGouZiGeVO> selectPageByEntity(int pageNo, int pageSize
        , ShouGouZiGeVO ziGeVO, String order) {
        PageHelper.startPage(pageNo, pageSize, order);
        return shouGouZiGeDao.selectPageByEntity(ziGeVO);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体的某些属性查询列表
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public List<ShouGouZiGeVO> selectByEntity(ShouGouZiGeVO ziGeVO) {
        return shouGouZiGeDao.selectByEntity(ziGeVO);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>根据实体的某些属性查询列表
     * @param entity    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    public List<ShouGouZiGeVO> selectByEntity(ShouGouZiGeDO entity) {
        ShouGouZiGeVO ziGeVO= JsonUtils.parse(
            JsonUtils.serialize(entity), ShouGouZiGeVO.class);
        return shouGouZiGeDao.selectByEntity(ziGeVO);
    }

}
