/*
 * ShouGouZiGeServiceImpl.java
 * 版权所有 © 2011-2017 北京神舟良实信息技术有限公司
 * Copyright © 2011-2017 All Rights Reserved    京ICP备13013562号
 *  created by lipenghe with LAPTOP-DPSC399A
 */
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.ShouGouZiGeDomain;
import com.unis.base.server.entity.ShouGouZiGeDO;
import com.unis.base.server.entity.TzSlqyDO;
import com.unis.base.server.service.ShouGouZiGeService;
import com.unis.common.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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 org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * TY_SHOUGOUZIGE-真收购资格表
 * @author lipenghe at 
 */
@Component
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Slf4j
public class ShouGouZiGeServiceImpl implements ShouGouZiGeService {

    @Autowired
    ShouGouZiGeDomain shouGouZiGeDomain;

    @Autowired
    CompanyDomain companyDomain;

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

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 插入全部字段
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ResponseWrapper insertEntityResult(ShouGouZiGeVO ziGeVO) {
        return shouGouZiGeDomain.insertEntity(ziGeVO);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 选择性插入字段
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ResponseWrapper insertEntitySelectiveResult(ShouGouZiGeVO ziGeVO) {
        return shouGouZiGeDomain.insertEntitySelective(ziGeVO, true);
    }

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

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 更改状态
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ResponseWrapper updateStatusByEntityResult(ShouGouZiGeVO ziGeVO, String limit) {
        return shouGouZiGeDomain.updateStatusByEntity(ziGeVO);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 根据实体某些属性更新全部字段
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ResponseWrapper updateByEntityResult(ShouGouZiGeVO ziGeVO, String limit) {
        return shouGouZiGeDomain.updateByEntity(ziGeVO);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 根据实体不为空的属性,选择性更新字段
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ResponseWrapper updateByEntitySelectiveResult(ShouGouZiGeVO ziGeVO, String limit) {
        return shouGouZiGeDomain.updateByEntitySelective(ziGeVO);
    }

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

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * [真删除]根据实体的某些属性进行单条或者批量删除
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ResponseWrapper deleteByEntityResult(ShouGouZiGeVO ziGeVO, String limit) {
        return shouGouZiGeDomain.deleteShamByEntity(ziGeVO);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 根据实体(唯一标示)查询一条数据
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ResponseWrapper selectByIdResult(ShouGouZiGeVO ziGeVO) {
        ShouGouZiGeVO ziGeVODb= shouGouZiGeDomain.selectById(ziGeVO);
        if(ziGeVODb!= null){
            ShouGouZiGeVO gouZiGeVO=new ShouGouZiGeVO();
            gouZiGeVO.setTyshxydm(ziGeVODb.getTyshxydm());
            List<ShouGouZiGeVO> gouZiGeVOList= shouGouZiGeDomain.selectByEntity(gouZiGeVO);
            if(gouZiGeVOList.size()>1){
                gouZiGeVOList.remove(0);
            }
            ziGeVODb.setShouGouZiGeVOList(gouZiGeVOList);

            String tyshxydm= ziGeVODb.getTyshxydm();
            CompanyQueryVO companyQueryVO=new  CompanyQueryVO();
            companyQueryVO.setTyshxydm(tyshxydm);
            TzSlqyDO tzSlqyDO= companyDomain.selectById(companyQueryVO);
            if(tzSlqyDO!=null){
                CompanyNewVO companyNewVO=JsonUtils.parse(JsonUtils.serialize(tzSlqyDO), CompanyNewVO.class);
                ziGeVODb.setCompanyNewVO(companyNewVO);
            }
            return ResponseWrapper.success(ziGeVODb);
        }
        return ResponseWrapper.notFound();
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 根据实体(唯一标示)查询一条数据
     * @param entity    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ResponseWrapper selectByIdResult(ShouGouZiGeDO entity) {
        ShouGouZiGeVO ziGeVO= shouGouZiGeDomain.selectById(entity);
        if(ziGeVO!= null){
            return ResponseWrapper.success(ziGeVO);
        }
        return ResponseWrapper.notFound();
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 根据实体的某些属性查询分页数据
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ResponseWrapper selectPageByEntityResult(int pageNo, int pageSize
        , ShouGouZiGeVO ziGeVO, String order) {
        Page<ShouGouZiGeVO> ziGeVOPage= shouGouZiGeDomain.selectPageByEntity(pageNo, pageSize, ziGeVO, order);
        PageInfo<ShouGouZiGeVO> ziGeVOPageInfo= new PageInfo<>(ziGeVOPage, 10);
        return ResponseWrapper.success(ziGeVOPageInfo);
    }

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

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

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 返回新的只有唯一标示的对象
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ShouGouZiGeVO getIdInstance(ShouGouZiGeVO ziGeVO) {
        ShouGouZiGeVO ziGeVONew= new ShouGouZiGeVO();
        ziGeVONew.setSgzgid(ziGeVO.getSgzgid());
        return ziGeVONew;
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 查询初始化数据
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ResponseWrapper selectInitDataResult(ShouGouZiGeVO ziGeVO) {
        return ResponseWrapper.success(ziGeVO);
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 根据条件导出数据
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public List<ShouGouZiGeVO> exportByEntityResult(ShouGouZiGeVO ziGeVO) {
        List<ShouGouZiGeVO> ziGeVOList= shouGouZiGeDomain.selectByEntity(ziGeVO);
        return ziGeVOList;
    }

    /**
     * <TY_SHOUGOUZIGE-真收购资格表>
     * 根据条件导入数据
     * @param ziGeVO    <真收购资格表>对象
     * @author lipenghe with LAPTOP-DPSC399A
     */
    @Override
    public ResponseWrapper importByEntityResult(ShouGouZiGeVO ziGeVO, List<MultipartFile> fileList, ImportConfigVO importConfigVO) throws IOException {
        List<ShouGouZiGeVO> ziGeVOList= null;
        //数据库字段集合
        LinkedHashMap<String, String> entityHeadMap= new LinkedHashMap();
        FieldUtil.getPoweredOrderedFields(entityHeadMap, new ShouGouZiGeVO(), new BaseEntity());

        List<Map<String, Object>> allSheetData= new ArrayList<>();
        ResponseWrapper responseWrapper= PoiModelExcelUtil.resloveExcel(fileList, entityHeadMap, allSheetData);
        if(responseWrapper.isSuccess()){
            for(Map<String, Object> map : allSheetData){
                String obj= (String)map.get("yxzt");
                String yyzzzch= (String)map.get("yyzzzch");
                if(StringUtils.isNotEmpty(yyzzzch)){
                    map.put("tyshxydm",yyzzzch);
                }
                map.put("status",1);
                if(StringUtils.isNotEmpty(obj)){
                    if("有效".equals(obj)){
                        map.put("yxzt",1);
                    }else if("无效".equals(obj)){
                        map.put("yxzt",0);
                    }else {
                        map.put("yxzt",1);
                    }
                }
            }
            ziGeVOList = JsonUtils.parseList(
                    JsonUtils.serialize(allSheetData), ShouGouZiGeVO.class);
            if(ziGeVOList!= null && ziGeVOList.size()> 0){
                int insertNum= 0;
                int updateNum= 0;
                int skipNum= 0;
                int failNum= 0;
                int size= ziGeVOList.size();
                for(ShouGouZiGeVO ziGeVOTemp : ziGeVOList){
                    int num= shouGouZiGeDomain.selectCountById(ziGeVOTemp);
                    System.out.println("num:"+num);
                    if(num== 0){
                        responseWrapper= shouGouZiGeDomain.insertEntitySelective(ziGeVOTemp, false);
                        if(responseWrapper.isSuccess()){
                            insertNum+= 1;
                        } else{
                            failNum+= 1;
                        }
                    } else if(num> 0 && importConfigVO!= null && !importConfigVO.isIgnoreExists()){
                        responseWrapper= shouGouZiGeDomain.updateByEntitySelective(ziGeVOTemp);
                        if(responseWrapper.isSuccess()){
                            updateNum+= 1;
                        } else{
                            failNum+= 1;
                        }
                    } else{
                        skipNum+= 1;
                    }
                }
                return ResponseWrapper.success("共"+size+",跳过"+skipNum+",失败"+failNum+",成功新增"+insertNum+",成功更新"+updateNum);
            } else{
                return ResponseWrapper.fail("未读取到有效数据或存在无法解析的属性");
            }
        }else{
            return responseWrapper;
        }
    }
}
