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

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.*;
import com.unis.base.server.domain.CompanyDomain;
import com.unis.base.server.domain.CompanyRecordsDomain;
import com.unis.base.server.domain.QybgsqDomain;
import com.unis.base.server.entity.CompanyRecordsDO;
import com.unis.base.server.entity.QybgsqDO;
import com.unis.base.server.entity.TzSlqyDO;
import com.unis.base.server.service.QybgsqService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TY_QYBGSQ-企业变更申请表
 * @author lipenghe at 
 */
@Component
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Slf4j
public class QybgsqServiceImpl implements QybgsqService {

    @Autowired
    QybgsqDomain qybgsqDomain;

    @Autowired
    CompanyDomain companyDomain;

    @Autowired
    CompanyRecordsDomain companyRecordsDomain;

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 选择性保存,包含新增和修改
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    public ResponseWrapper saveEntitySelectiveResult(QybgsqVO qybgsqVO) {
        return qybgsqDomain.saveEntitySelective(qybgsqVO);
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 插入全部字段
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper insertEntityResult(QybgsqVO qybgsqVO) {
        return qybgsqDomain.insertEntity(qybgsqVO);
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 选择性插入字段
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper insertEntitySelectiveResult(QybgsqVO qybgsqVO) {
        return qybgsqDomain.insertEntitySelective(qybgsqVO, true);
    }

   /**
    * <TY_QYBGSQ-企业变更申请表>批量插入全部字段
    * @param qybgsqVOList    <企业变更申请表>对象列表
    * @author lipenghe with LAPTOP-IJQ2SDFV
    */
    public ResponseWrapper insertBatchEntityListResult(List<QybgsqVO> qybgsqVOList) {
        int num= qybgsqDomain.insertBatchEntityList(qybgsqVOList);
        return ResponseWrapper.choose(num);
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 更改状态
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper updateStatusByEntityResult(QybgsqVO qybgsqVO, String limit) {
        return qybgsqDomain.updateStatusByEntity(qybgsqVO);
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 根据实体某些属性更新全部字段
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper updateByEntityResult(QybgsqVO qybgsqVO, String limit) {
        return qybgsqDomain.updateByEntity(qybgsqVO);
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 根据实体不为空的属性,选择性更新字段
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper updateByEntitySelectiveResult(QybgsqVO qybgsqVO, String limit) {
        //企业变更申请审核
        ResponseWrapper responseWrapper= qybgsqDomain.updateByEntitySelective(qybgsqVO);
        if(responseWrapper.isSuccess() && "1".equals(qybgsqVO.getSfty())){
            String sqqybm=qybgsqVO.getSqqybm();
            CompanyQueryVO companyVO=new CompanyQueryVO();
            companyVO.setTyshxydm(qybgsqVO.getTyshxydm());
            TzSlqyDO tzSlqyDO= companyDomain.selectById(companyVO);
            if(tzSlqyDO!=null){
                //备份历史数据
                CompanyRecordsNewVO companyRecordsVO= JsonUtils.parse(JsonUtils.serialize(tzSlqyDO), CompanyRecordsNewVO.class);
                companyRecordsDomain.insert(companyRecordsVO);

                //修改数据
                tzSlqyDO.setSslsxzglbm(sqqybm);
                CompanyNewVO companyVONew = JsonUtils.parse(JsonUtils.serialize(tzSlqyDO), CompanyNewVO.class);
                companyDomain.update(companyVONew);
            }
        }
        return responseWrapper;
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 根据实体不为空的属性,选择性更新字段
     * @param entity    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper updateByEntitySelectiveResult(QybgsqDO entity, String limit) {
        int num= qybgsqDomain.updateByEntitySelective(entity);
        return ResponseWrapper.choose(num);
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * [真删除]根据实体的某些属性进行单条或者批量删除
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper deleteByEntityResult(QybgsqVO qybgsqVO, String limit) {
        return qybgsqDomain.deleteShamByEntity(qybgsqVO);
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 根据实体(唯一标示)查询一条数据
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper selectByIdResult(QybgsqVO qybgsqVO) {
        QybgsqVO qybgsqVODb= qybgsqDomain.selectById(qybgsqVO);
        if(qybgsqVODb!= null){
            return ResponseWrapper.success(qybgsqVODb);
        }
        return ResponseWrapper.notFound();
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 根据实体(唯一标示)查询一条数据
     * @param entity    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper selectByIdResult(QybgsqDO entity) {
        QybgsqVO qybgsqVO= qybgsqDomain.selectById(entity);
        if(qybgsqVO!= null){
            return ResponseWrapper.success(qybgsqVO);
        }
        return ResponseWrapper.notFound();
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 根据实体的某些属性查询分页数据
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper selectPageByEntityResult(int pageNo, int pageSize
        , QybgsqVO qybgsqVO, String order) {
        Page<QybgsqVO> qybgsqVOPage= qybgsqDomain.selectPageByEntity(pageNo, pageSize, qybgsqVO, order);
        PageInfo<QybgsqVO> qybgsqVOPageInfo= new PageInfo<>(qybgsqVOPage, 10);
        return ResponseWrapper.success(qybgsqVOPageInfo);
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 根据实体的某些属性查询列表
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper selectByEntityResult(QybgsqVO qybgsqVO) {
        List<QybgsqVO> qybgsqVOList= qybgsqDomain.selectByEntity(qybgsqVO);
        return ResponseWrapper.success(qybgsqVOList);
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 根据实体的某些属性查询列表
     * @param entity    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper selectByEntityResult(QybgsqDO entity) {
        List<QybgsqVO> qybgsqVOList= qybgsqDomain.selectByEntity(entity);
        return ResponseWrapper.success(qybgsqVOList);
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 返回新的只有唯一标示的对象
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public QybgsqVO getIdInstance(QybgsqVO qybgsqVO) {
        QybgsqVO qybgsqVONew= new QybgsqVO();
        qybgsqVONew.setSqid(qybgsqVO.getSqid());
        return qybgsqVONew;
    }

    /**
     * <TY_QYBGSQ-企业变更申请表>
     * 查询初始化数据
     * @param qybgsqVO    <企业变更申请表>对象
     * @author lipenghe with LAPTOP-IJQ2SDFV
     */
    @Override
    public ResponseWrapper selectInitDataResult(QybgsqVO qybgsqVO) {
        return ResponseWrapper.success(qybgsqVO);
    }

}
