package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.common.support.util.StringUtils;
import com.yh.saas.plugin.base.service.ICommonBillOperateHisService;
import com.yh.saas.plugin.yiliangyiyun.constant.NumberConstant;
import com.yh.saas.plugin.yiliangyiyun.constant.StatusEnum;
import com.yh.saas.plugin.yiliangyiyun.entity.*;
import com.yh.saas.plugin.yiliangyiyun.exception.*;
import com.yh.saas.plugin.yiliangyiyun.mapper.IdentityAuthenticationInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

/**
 * <p>
 * 记录身份认证 服务实现类
 * </p>
 *
 * @author Gongdc
 * @since 2021-07-23
 */
@Service
public class IdentityAuthenticationInfoServiceImpl extends ServiceImpl<IdentityAuthenticationInfoMapper, IdentityAuthenticationInfo> implements IIdentityAuthenticationInfoService {
    @Autowired
    private ICommonBillOperateHisService billOperateHisService;
    @Autowired
    private ICommonCompanyIdentityService iCommonCompanyIdentityService;
    @Autowired
    private ICustomerInfoService customerInfoService;
    @Autowired
    private IPurchasePriceService purchasePriceService;
    @Autowired
    private IPaymentManagementService paymentManagementService;
    @Autowired
    private IShippingInformationService shippingInformationService;

    /**
     * 粮商认证列表
     *
     * @param identityAuthenticationInfo
     * @return
     */
    public Page<IdentityAuthenticationInfo> selectIdentityAuthenticationInfo(IdentityAuthenticationInfo identityAuthenticationInfo) {
        Wrapper<IdentityAuthenticationInfo> identityAuthenticationInfoWrapper = new EntityWrapper<IdentityAuthenticationInfo>();
        //切换身份页面
        if (identityAuthenticationInfo.getFlag() == 1) {
            identityAuthenticationInfoWrapper.eq("authentication_status_key", "7")
                    .eq(IdentityAuthenticationInfo.QueryFiles.COMMON_ID, identityAuthenticationInfo.getCommonId())
                    .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0)
                    .eq("cover", NumberConstant.CONSTANT0)
                    .orderBy("update_date", false);
        }
        //列表
        else {
            identityAuthenticationInfoWrapper.eq(IdentityAuthenticationInfo.QueryFiles.COMMON_ID, identityAuthenticationInfo.getCommonId())
                    .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0)
                    .orderBy("update_date", false);
        }
        Page<IdentityAuthenticationInfo> recommendedIdentityAuthenticationInfoPage = this.selectPage(identityAuthenticationInfo.getQueryPage(), identityAuthenticationInfoWrapper);
        List<IdentityAuthenticationInfo> identityAuthenticationInfoList = recommendedIdentityAuthenticationInfoPage.getRecords();
        //扫码列表
        if (identityAuthenticationInfo.getFlag() == 3) {
            if (CollectionUtils.isNotEmpty(identityAuthenticationInfoList)) {
                for (IdentityAuthenticationInfo identityAuthenticationInfo1 : identityAuthenticationInfoList) {
                    CommonCompanyIdentity commonCompanyIdentity = iCommonCompanyIdentityService.selectOne(new EntityWrapper<CommonCompanyIdentity>()
                            .eq("com_id", identityAuthenticationInfo.getCompId()).eq("identity_id", identityAuthenticationInfo1.getId()));
                    if (commonCompanyIdentity != null) {
                        identityAuthenticationInfo1.setAuthenticationStatus("已接受");
                    }
                    //筛除企业
                    if("2".equals(identityAuthenticationInfo1.getCustomerTypeFlag())){
                        identityAuthenticationInfoList.remove(identityAuthenticationInfo1);
                    }
                }
            }
        }
        recommendedIdentityAuthenticationInfoPage.setRecords(identityAuthenticationInfoList);
        return recommendedIdentityAuthenticationInfoPage;
    }

    /**
     * 更改手机号
     *
     * @param identityAuthenticationInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String modifyIdentityPhone(IdentityAuthenticationInfo identityAuthenticationInfo) {
        IdentityAuthenticationInfo identityAuthenticationInfo2 = this.selectById(identityAuthenticationInfo.getId());
        //根据原来的手机号查到客户表
        CustomerInfo customerInfo=customerInfoService.selectOne(new EntityWrapper<CustomerInfo>()
                .eq("customer_phone", identityAuthenticationInfo2.getCustomerPhone())
                .eq("delete_flag", "0"));
        if(customerInfo!=null){
            customerInfo.setCustomerPhone(identityAuthenticationInfo.getCustomerPhone());
            //更新客户表
            customerInfoService.updateById(customerInfo);
        }
        //已覆盖的粮商解除覆盖状态
        if ("1".equals(identityAuthenticationInfo2.getCover())) {
            identityAuthenticationInfo2.setCover("0");
        }
        IdentityAuthenticationInfo identityAuthenticationInfo1 = this.selectOne(new EntityWrapper<IdentityAuthenticationInfo>()
                .eq("customer_phone", identityAuthenticationInfo.getCustomerPhone())
                .eq("customer_type_flag", identityAuthenticationInfo2.getCustomerTypeFlag())
                .eq("delete_flag", "0"));
        //查询手机号存在，将其覆盖
        if (identityAuthenticationInfo1 != null) {
            identityAuthenticationInfo1.setCover("1");
            identityAuthenticationInfo1.setDeleteFlag("1");
            this.updateById(identityAuthenticationInfo1);
        }
        identityAuthenticationInfo2.setCustomerPhone(identityAuthenticationInfo.getCustomerPhone());
        //修改身份认证信息
        this.updateById(identityAuthenticationInfo2);
        return "OK";
    }

    /**
     * 添加身份认证
     *
     * @param identityAuthenticationInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addIdentityAuthenticationInfo(IdentityAuthenticationInfo identityAuthenticationInfo) throws ServiceException {
        // 根据传参手机号判断手机号是否存在
        //  空是新增
        identityAuthenticationInfo.setId(IdGenerator.generateUUID());
        //pc创建的客户不需审核
        if(identityAuthenticationInfo.getCompId()!=null){
            identityAuthenticationInfo.setAuthenticationStatus(StatusEnum.IDENTITY_COMPLETED.getName());
            identityAuthenticationInfo.setAuthenticationStatusKey(StatusEnum.IDENTITY_COMPLETED.getFlag());
        }
        //粮农身份不需审核
        else if ("3".equals(identityAuthenticationInfo.getCustomerTypeFlag())) {
            identityAuthenticationInfo.setAuthenticationStatus(StatusEnum.IDENTITY_COMPLETED.getName());
            identityAuthenticationInfo.setAuthenticationStatusKey(StatusEnum.IDENTITY_COMPLETED.getFlag());
        } else {
            identityAuthenticationInfo.setAuthenticationStatus(StatusEnum.IDENTITY_REVIEWED.getName());
            identityAuthenticationInfo.setAuthenticationStatusKey(StatusEnum.IDENTITY_REVIEWED.getFlag());
        }
        //判断手机号是否唯一
        List<IdentityAuthenticationInfo> identityAuthenticationInfoList =
                this.selectList(new EntityWrapper<IdentityAuthenticationInfo>()
                        .eq(IdentityAuthenticationInfo.QueryFiles.CUSTOMER_PHONE, identityAuthenticationInfo.getCustomerPhone())
                        .eq("customer_name", identityAuthenticationInfo.getCustomerName())
                        .eq("comp_id", identityAuthenticationInfo.getCompId())
                        .eq("customer_type_flag", identityAuthenticationInfo.getCustomerTypeFlag())
                        .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
        //同一手机号再次被认证
        if (identityAuthenticationInfoList.size() > 0) {
            for (IdentityAuthenticationInfo identityAuthenticationInfo1 : identityAuthenticationInfoList) {
                identityAuthenticationInfo1.setCover("1");
                identityAuthenticationInfo1.setDeleteFlag("1");
                this.updateById(identityAuthenticationInfo1);
            }
        }
        // 操作主表数据
        this.insert(identityAuthenticationInfo);
        //如果是个人粮商且公司id不为空，则添加公司与个人粮商关系表
        if ("1".equals(identityAuthenticationInfo.getCustomerTypeFlag()) && identityAuthenticationInfo.getCompId() != null && !identityAuthenticationInfo.getCompId().isEmpty()) {
            this.saveCompanyIdentity(identityAuthenticationInfo);
        }
        return identityAuthenticationInfo.getId();
    }

    /**
     * 编辑身份认证
     *
     * @param identityAuthenticationInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editIdentityAuthenticationInfo(IdentityAuthenticationInfo identityAuthenticationInfo) {
        boolean one = this.updateById(identityAuthenticationInfo);
        //成功返回ok
        if (one) {
            return "OK";
        } else {
            return "NG";
        }
    }

    /**
     * 查看身份认证
     *
     * @param id
     * @return
     */
    @Override
    public IdentityAuthenticationInfo getIdentityAuthenticationInfo(String id) {
        //查看身份认证
        IdentityAuthenticationInfo identityAuthenticationInfo = this.selectById(id);
        return identityAuthenticationInfo;
    }

    /**
     * 删除身份认证
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteIdentityAuthenticationInfo(String id) {
        //查询身份认证信息
        IdentityAuthenticationInfo identityAuthenticationInfo = this.selectById(id);
        if (identityAuthenticationInfo != null) {
            //删除身份认证
            this.deleteById(identityAuthenticationInfo.getId());
        }
    }

    /**
     * 查看
     *
     * @param
     */
    @Override
    public IdentityAuthenticationInfo getInfo(String commonId) throws ServiceException {
        //判断粮商是否存在
        IdentityAuthenticationInfo identityAuthenticationInfo =
                this.selectOne(new EntityWrapper<IdentityAuthenticationInfo>()
                        .eq(IdentityAuthenticationInfo.QueryFiles.COMMON_ID, commonId)
                        .eq("cover", NumberConstant.CONSTANT0)
                        .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
        if (identityAuthenticationInfo != null) {
            //判断粮商是否存在
            IdentityAuthenticationInfo identityAuthenticationInfo1 =
                    this.selectOne(new EntityWrapper<IdentityAuthenticationInfo>()
                            .eq(IdentityAuthenticationInfo.QueryFiles.COMMON_ID, commonId)
                            .eq("authentication_status_key", "7")
                            .eq("cover", NumberConstant.CONSTANT0)
                            .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
            //身份审核中
            if (identityAuthenticationInfo1 == null) {
                throw new AppServiceException(ExceptionDefinition.IDENTITY_IS_BEING_AUDITED);
            }
            IdentityAuthenticationInfo identityAuthenticationInfos =
                    this.selectOne(new EntityWrapper<IdentityAuthenticationInfo>()
                            .eq(IdentityAuthenticationInfo.QueryFiles.COMMON_ID, commonId)
                            .eq("authentication_status_key", "7")
                            .eq("cover", NumberConstant.CONSTANT0)
                            .eq("default_flag", "1")
                            .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
            //有默认
            if (identityAuthenticationInfos != null) {
                return identityAuthenticationInfos;
            } else {
                return identityAuthenticationInfo1;
            }

        }
        //粮商不存在
        else {
            throw new AppServiceException(ExceptionDefinition.UNAUTHENTICATED_GRAIN_MERCHANT_IDENTITY);
        }
    }

    /**
     * 审核
     *
     * @param identityAuthenticationInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String examine(IdentityAuthenticationInfo identityAuthenticationInfo) {
        //查询粮商列表
        IdentityAuthenticationInfo identityAuthenticationInfo1 = this.selectById(identityAuthenticationInfo.getId());
        if (identityAuthenticationInfo1 != null) {
            //通过
            if (identityAuthenticationInfo.getFlag() == 1) {
                identityAuthenticationInfo1.setAuthenticationStatusKey(StatusEnum.IDENTITY_COMPLETED.getFlag());
                identityAuthenticationInfo1.setAuthenticationStatus(StatusEnum.IDENTITY_COMPLETED.getName());
            }
            //驳回
            else {
                identityAuthenticationInfo1.setAuthenticationStatusKey(StatusEnum.IDENTITY_FAILED.getFlag());
                identityAuthenticationInfo1.setAuthenticationStatus(StatusEnum.IDENTITY_FAILED.getName());
            }
            //更改粮商信息
            this.updateById(identityAuthenticationInfo1);
            return "OK";
        }
        return "NG";
    }

    /**
     * 设置默认
     *
     * @param identityAuthenticationInfo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String setDefault(IdentityAuthenticationInfo identityAuthenticationInfo) {
        //查询是否有默认身份
        IdentityAuthenticationInfo identityAuthenticationInfo1 = this.selectOne(new EntityWrapper<IdentityAuthenticationInfo>()
                .eq("common_id", identityAuthenticationInfo.getCommonId())
                .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0)
                .eq(IdentityAuthenticationInfo.QueryFiles.DEFAULT_FLAG, NumberConstant.CONSTANT1));
        if (identityAuthenticationInfo1 != null) {
            //取消默认
            identityAuthenticationInfo1.setDefaultFlag("0");
            this.updateById(identityAuthenticationInfo1);
        }
        identityAuthenticationInfo.setDefaultFlag("1");
        boolean one = this.updateById(identityAuthenticationInfo);
        //成功返回ok
        if (one) {
            return "OK";
        } else {
            return "NG";
        }
    }

    /**
     * 粮商认证列表PC端
     *
     * @param identityAuthenticationInfo
     */
    @Override
    public Page<IdentityAuthenticationInfo> selectIdentityAuthenticationInfoPc(IdentityAuthenticationInfo identityAuthenticationInfo) {
        Wrapper<IdentityAuthenticationInfo> identityAuthenticationInfoWrapper = new EntityWrapper<IdentityAuthenticationInfo>();
        //待审核
        if ("1".equals(identityAuthenticationInfo.getSearchType())) {
            identityAuthenticationInfoWrapper.eq("authentication_status_key", "1")
//                    .eq(IdentityAuthenticationInfo.QueryFiles.COMP_ID,identityAuthenticationInfo.getCompId())
                    .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0);
            identityAuthenticationInfoWrapper.andNew("replace(" + "customer_name" + ",' ','')like {0} or "
                    + "customer_phone" + " like {0} or "
                    + "customer_number_card" + " like {0}", "%" + identityAuthenticationInfo.getSearchKeyWord() + "%");
            identityAuthenticationInfoWrapper.orderBy("update_date", false);
        }
        //已通过
        else if ("2".equals(identityAuthenticationInfo.getSearchType())) {
            identityAuthenticationInfoWrapper.eq("authentication_status_key", "7")
//                    .eq(IdentityAuthenticationInfo.QueryFiles.COMP_ID,identityAuthenticationInfo.getCompId())
                    .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0);
            identityAuthenticationInfoWrapper.andNew("replace(" + "customer_name" + ",' ','')like {0} or "
                    + "customer_phone" + " like {0} or "
                    + "customer_number_card" + " like {0}", "%" + identityAuthenticationInfo.getSearchKeyWord() + "%");
            identityAuthenticationInfoWrapper.orderBy("update_date", false);
        }
        //全部
        else {
//            identityAuthenticationInfoWrapper.eq(IdentityAuthenticationInfo.QueryFiles.COMP_ID,identityAuthenticationInfo.getCompId())
            identityAuthenticationInfoWrapper
                    .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0);
            identityAuthenticationInfoWrapper.andNew("replace(" + "customer_name" + ",' ','')like {0} or "
                    + "customer_phone" + " like {0} or "
                    + "customer_number_card" + " like {0}", "%" + identityAuthenticationInfo.getSearchKeyWord() + "%");
            identityAuthenticationInfoWrapper.orderBy("update_date", false);
        }
        Page<IdentityAuthenticationInfo> recommendedIdentityAuthenticationInfoPage = this.selectPage(identityAuthenticationInfo.getQueryPage(), identityAuthenticationInfoWrapper);
        List<IdentityAuthenticationInfo> identityAuthenticationInfoList = recommendedIdentityAuthenticationInfoPage.getRecords();
        recommendedIdentityAuthenticationInfoPage.setRecords(identityAuthenticationInfoList);
        return recommendedIdentityAuthenticationInfoPage;
    }

    /**
     * 查粮农身份
     *
     * @param identityAuthenticationInfo
     */
    @Override
    public IdentityAuthenticationInfo selectCount(IdentityAuthenticationInfo identityAuthenticationInfo) {
        IdentityAuthenticationInfo identityAuthenticationInfo1 = this.selectOne(new EntityWrapper<IdentityAuthenticationInfo>()
                .eq("customer_type_flag", "3")
                .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
        return identityAuthenticationInfo1;
    }

    /**
     * 查手机号是否存在
     *
     * @param identityAuthenticationInfo
     */
    @Override
    public IdentityAuthenticationInfo selectPhone(IdentityAuthenticationInfo identityAuthenticationInfo) throws AppServiceException {
        //未覆盖的手机号
        IdentityAuthenticationInfo identityAuthenticationInfo1 = this.selectOne(new EntityWrapper<IdentityAuthenticationInfo>()
                .eq(IdentityAuthenticationInfo.QueryFiles.CUSTOMER_PHONE, identityAuthenticationInfo.getCustomerPhone())
                .eq("cover", "0")
                .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
        if (identityAuthenticationInfo1 != null) {
            throw new AppServiceException(ExceptionDefinition.PHONE_NUMBER_ALREADY_EXISTS);
        }
        return identityAuthenticationInfo1;
    }

    @Override
    public Page<IdentityAuthenticationInfo> selectIdentityAuthenticationInfoPurchase(IdentityAuthenticationInfo identityAuthenticationInfo) {
        try {
            Map<String, Object> pageView = new HashMap<>();
            pageView.put("startRecord", (identityAuthenticationInfo.getCurrentPage() - 1)
                    * identityAuthenticationInfo.getPageSize());
            //公司id
            pageView.put("compId", identityAuthenticationInfo.getCompId());
            pageView.put("searchKeyWord", identityAuthenticationInfo.getSearchKeyWord());
            pageView.put("pageSize", identityAuthenticationInfo.getPageSize());
            pageView.put("currentPage", identityAuthenticationInfo.getCurrentPage());
            Calendar cal = Calendar.getInstance();
            int yearNum = cal.get(Calendar.YEAR);
//            int month = cal.get(Calendar.MONTH) + 1;
//            if(month < 9){
//                yearNum --;
//            }

            SimpleDateFormat now = new SimpleDateFormat(yearNum +"-01-01 00:00:00");
            Calendar c = Calendar.getInstance();
            c.setTime(new java.util.Date());
            c.add(Calendar.YEAR, 0);
            Date y = c.getTime();
            String year = now.format(y);
            //公司id
            pageView.put("year", year);
            List<IdentityAuthenticationInfo> dataList = this.baseMapper.getIdentityInfoByCondition(pageView);


            Integer dataCount = this.baseMapper.getIdentityCountByCondition(pageView);

            Page<IdentityAuthenticationInfo> page = new Page<>();
            page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
            page.setTotal(dataCount == null ? 0 : dataCount);
            page.setCurrent(identityAuthenticationInfo.getCurrentPage());
            page.setSize(identityAuthenticationInfo.getPageSize());
            return page;

        } catch (Exception e) {
            System.out.println("查询异常:" + e.toString());
            return null;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveCompanyIdentity(IdentityAuthenticationInfo identityAuthenticationInfo) {
        IdentityAuthenticationInfo identityAuthenticationInfo1=this.selectById(identityAuthenticationInfo.getId());
        CommonCompanyIdentity commonCompanyIdentity = new CommonCompanyIdentity();
        commonCompanyIdentity.setIdentityId(identityAuthenticationInfo1.getId());
        commonCompanyIdentity.setComId(identityAuthenticationInfo1.getCompId()!=null?identityAuthenticationInfo1.getCompId():identityAuthenticationInfo.getCompId());

        Boolean one = iCommonCompanyIdentityService.insert(commonCompanyIdentity);
        //成功返回ok
        if (one ) {
            return "OK";
        } else {
            return "NG";
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCompanyIdentity(IdentityAuthenticationInfo identityAuthenticationInfo) {
        List<CommonCompanyIdentity> list = this.iCommonCompanyIdentityService.selectList(new EntityWrapper<CommonCompanyIdentity>()
                .eq("com_id", identityAuthenticationInfo.getCompId()).eq("identity_id", identityAuthenticationInfo.getId()));
        if (!CollectionUtils.isEmpty(list)){
            this.iCommonCompanyIdentityService.delete(new EntityWrapper<CommonCompanyIdentity>()
                    .eq("com_id", identityAuthenticationInfo.getCompId()).eq("identity_id", identityAuthenticationInfo.getId()));
        }
    }

    /**
     * 自采客户下拉列表
     * @param identityAuthenticationInfo
     * @return
     */
    @Override
    public List<IdentityAuthenticationInfo> identityAuthenticationInfoCustomer(IdentityAuthenticationInfo identityAuthenticationInfo) {
        Map<String, Object> pageView = new HashMap<>();
        if(identityAuthenticationInfo.getCompId() == null || identityAuthenticationInfo.getCompId().isEmpty()){
            identityAuthenticationInfo.setCompId(AuthSecurityUtils.getStaffById(AuthSecurityUtils.getCurrentUserId()).getCompId());
        }
        PurchasePrice purchasePrice=purchasePriceService.selectOne(new EntityWrapper<PurchasePrice>()
                .eq("warehouse_id",identityAuthenticationInfo.getWarehouseId())
                .eq("goods_name",identityAuthenticationInfo.getGoodsName())
                .eq("status","已通过"));
        if (identityAuthenticationInfo.getGoodsName().contains("玉米")) {
            identityAuthenticationInfo.setGoodsName("玉米");
        }
        Calendar cal = Calendar.getInstance();
        int yearNum = cal.get(Calendar.YEAR);
//        int month = cal.get(Calendar.MONTH) + 1;
//        if(month < 9){
//            yearNum --;
//        }

        SimpleDateFormat now = new SimpleDateFormat(yearNum +"-01-01 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(new java.util.Date());
        c.add(Calendar.YEAR, 0);
        Date y = c.getTime();
        String year = now.format(y);
        //公司id
        pageView.put("compId", identityAuthenticationInfo.getCompId());
        pageView.put("authenticationStatusKey", "7");
        pageView.put("year", year);
        pageView.put("goodsName",identityAuthenticationInfo.getGoodsName());
        pageView.put("saleLimit", purchasePrice.getSaleLimit()*1000);
        pageView.put("searchKeyWord", identityAuthenticationInfo.getSearchKeyWord());
        List<IdentityAuthenticationInfo> dataList = this.baseMapper.getIdentityInfoNotLimit(pageView);
        return dataList;
//        if(identityAuthenticationInfo.getCompId() == null || identityAuthenticationInfo.getCompId().isEmpty()){
//            identityAuthenticationInfo.setCompId(AuthSecurityUtils.getStaffById(AuthSecurityUtils.getCurrentUserId()).getCompId());
//        }
//        Wrapper<IdentityAuthenticationInfo> identityAuthenticationInfoWrapper = new EntityWrapper<>();
//        identityAuthenticationInfoWrapper.eq("comp_id",identityAuthenticationInfo.getCompId())
//                .eq("cover","0")
//                .eq("authentication_status_key","7");
//        if (!StringUtils.isEmpty(identityAuthenticationInfo.getSearchKeyWord())) {
//            identityAuthenticationInfoWrapper.andNew("replace(" + "customer_name" + ",' ','')like {0} or "
//                    + "customer_phone" + " like {0} or "
//                    + "supplier" + " like {0}", "%" + identityAuthenticationInfo.getSearchKeyWord() + "%");
//        }
//        List<IdentityAuthenticationInfo> identityAuthenticationInfoList = this.selectList(identityAuthenticationInfoWrapper);
//        List<IdentityAuthenticationInfo> identityAuthenticationInfoListNew=new ArrayList<>();
//        if (CollectionUtils.isNotEmpty(identityAuthenticationInfoList)){
//            for (IdentityAuthenticationInfo identityAuthenticationInfo1:identityAuthenticationInfoList) {
//                Float cumulant = 0.0f;
//                //本年第一天零点
//                SimpleDateFormat now = new SimpleDateFormat("yyyy-01-01 00:00:00");
//                Calendar c = Calendar.getInstance();
//                c.setTime(new java.util.Date());
//                c.add(Calendar.YEAR, 0);
//                Date y = c.getTime();
//                String year = now.format(y);
//                if (identityAuthenticationInfo.getGoodsName().contains("玉米")) {
//                    identityAuthenticationInfo.setGoodsName("玉米");
//                }
//                List<PaymentManagement> paymentManagementList = paymentManagementService.selectList(new EntityWrapper<PaymentManagement>()
//                        .like("goods_name", "%" + identityAuthenticationInfo.getGoodsName() + "%")
//                        .eq("customer_number_card", identityAuthenticationInfo1.getCustomerNumberCard())
//                        .eq("comp_id", purchasePrice.getCompId())
//                        .ge("create_date", year));
//                if (!CollectionUtils.isEmpty(paymentManagementList)) {
//                    Float netWeight = 0f;
//                    for (PaymentManagement paymentManagement1 : paymentManagementList) {
//                        //干粮净重
//                        if ("干粮".equals(paymentManagement1.getType())) {
//                            netWeight = netWeight + (paymentManagement1.getNetWeight() != null ? paymentManagement1.getNetWeight() : 0f);
//                        }
//                        //潮粮纯重
//                        else if ("潮粮".equals(paymentManagement1.getType())) {
//                            netWeight = netWeight + (paymentManagement1.getPureWeight() != null ? paymentManagement1.getPureWeight() : 0f);
//                        }
//                    }
//                    cumulant = cumulant + netWeight;
//                }
//                if (cumulant > (purchasePrice.getSaleLimit() - 50)*1000) {
//                    identityAuthenticationInfoListNew.add(identityAuthenticationInfo1);
//                }
//            }
//            identityAuthenticationInfoList.removeAll(identityAuthenticationInfoListNew);
//        }
//        return identityAuthenticationInfoList;
    }

    /**
     * 送粮登记货主下拉列表
     * @param identityAuthenticationInfo
     * @return
     */
    @Override
    public List<IdentityAuthenticationInfo> identityAuthenticationInfoUser(IdentityAuthenticationInfo identityAuthenticationInfo) throws AppServiceException {
        List<IdentityAuthenticationInfo> identityAuthenticationInfoList = this.selectList(new EntityWrapper<IdentityAuthenticationInfo>()
                .eq("common_id",identityAuthenticationInfo.getCommonId())
                .eq("cover","0")
                .eq(IdentityAuthenticationInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0)
                .eq("authentication_status_key","7"));
        if (CollectionUtils.isEmpty(identityAuthenticationInfoList)) {
            throw new AppServiceException(ExceptionDefinition.UNAUTHENTICATED_IDENTITY);
        }else {
            PurchasePrice purchasePrice = purchasePriceService.selectOne(new EntityWrapper<PurchasePrice>()
                    .eq("warehouse_id", identityAuthenticationInfo.getWarehouseId())
                    .eq("goods_name", identityAuthenticationInfo.getGoodsName())
                    .eq("status", "已通过"));
            List<IdentityAuthenticationInfo> identityAuthenticationInfoListNew = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(identityAuthenticationInfoList)) {
                for (IdentityAuthenticationInfo identityAuthenticationInfo1 : identityAuthenticationInfoList) {
                    Float cumulant = 0.0f;
                    //本年第一天零点
                    SimpleDateFormat now = new SimpleDateFormat("yyyy-01-01 00:00:00");
                    Calendar c = Calendar.getInstance();
                    c.setTime(new java.util.Date());
                    c.add(Calendar.YEAR, 0);
                    Date y = c.getTime();
                    String year = now.format(y);
                    if (identityAuthenticationInfo.getGoodsName().contains("玉米")) {
                        identityAuthenticationInfo.setGoodsName("玉米");
                    }
                    List<PaymentManagement> paymentManagementList = paymentManagementService.selectList(new EntityWrapper<PaymentManagement>()
                            .like("goods_name", "%" + identityAuthenticationInfo.getGoodsName() + "%")
                            .eq("customer_number_card", identityAuthenticationInfo1.getCustomerNumberCard())
                            .eq("comp_id", purchasePrice.getCompId())
                            .ge("create_date", year));
                    if (!CollectionUtils.isEmpty(paymentManagementList)) {
                        Float netWeight = 0f;
                        for (PaymentManagement paymentManagement1 : paymentManagementList) {
                            //干粮净重
                            if ("干粮".equals(paymentManagement1.getType())) {
                                netWeight = netWeight + (paymentManagement1.getNetWeight() != null ? paymentManagement1.getNetWeight() : 0f);
                            }
                            //潮粮纯重
                            else if ("潮粮".equals(paymentManagement1.getType())) {
                                netWeight = netWeight + (paymentManagement1.getPureWeight() != null ? paymentManagement1.getPureWeight() : 0f);
                            }
                        }
                        cumulant = cumulant + netWeight;
                    }
                    if (cumulant > (purchasePrice.getSaleLimit() - 50) * 1000) {
                        identityAuthenticationInfoListNew.add(identityAuthenticationInfo1);
                    }
                }
                identityAuthenticationInfoList.removeAll(identityAuthenticationInfoListNew);
            }
        }
        return identityAuthenticationInfoList;
    }
}

