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

import com.baomidou.mybatisplus.mapper.EntityWrapper;
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.plugin.yiliangyiyun.entity.*;
import com.yh.saas.plugin.yiliangyiyun.mapper.GrainDeliveryRegistrationMapper;
import com.yh.saas.plugin.yiliangyiyun.service.ICommonCompanyIdentityService;
import com.yh.saas.plugin.yiliangyiyun.service.IGrainDeliveryRegistrationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.yiliangyiyun.service.IIdentityAuthenticationInfoService;
import com.yh.saas.plugin.yiliangyiyun.service.IShippingInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * 送粮登记管理 服务实现类
 * </p>
 *
 * @author Gdc
 * @since 2021-11-26
 */
@Service
public class GrainDeliveryRegistrationServiceImpl extends ServiceImpl<GrainDeliveryRegistrationMapper, GrainDeliveryRegistration> implements IGrainDeliveryRegistrationService {

    @Autowired
    private IIdentityAuthenticationInfoService identityAuthenticationInfoService;
    @Autowired
    private ICommonCompanyIdentityService iCommonCompanyIdentityService;
    @Autowired
    private IShippingInformationService shippingInformationService;

    /**
     * 送粮登记
     */
    @Override
    public String insertInfo(GrainDeliveryRegistration grainDeliveryRegistration){
        //新增主键id
        grainDeliveryRegistration.setId(IdGenerator.generateUUID());
        grainDeliveryRegistration.setCompId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
//        Calendar calendar = new GregorianCalendar();
//        calendar.setTime(new Date());
//        calendar.add(calendar.DATE,2);
//        grainDeliveryRegistration.setValidityDate(calendar.getTime());
        List<ShippingInformation> shippingInformationList = grainDeliveryRegistration.getShippingInformationList();
        if (!CollectionUtils.isEmpty(shippingInformationList)) {
            for (ShippingInformation shippingInformation : shippingInformationList) {
                IdentityAuthenticationInfo identityAuthenticationInfo = identityAuthenticationInfoService.selectById(shippingInformation.getIdentityId());
                if (identityAuthenticationInfo != null){
                    CommonCompanyIdentity commonCompanyIdentity = iCommonCompanyIdentityService.selectOne(
                            new EntityWrapper<CommonCompanyIdentity>()
                                    .eq("identity_id",identityAuthenticationInfo.getId())
                                    .eq("com_id",grainDeliveryRegistration.getCompId())
                    );
                    //添加公司与个人粮商关系表
                    if ("1".equals(identityAuthenticationInfo.getCustomerTypeFlag()) && commonCompanyIdentity == null) {
                        commonCompanyIdentity = new CommonCompanyIdentity();
                        commonCompanyIdentity.setIdentityId(identityAuthenticationInfo.getId());
                        commonCompanyIdentity.setComId(grainDeliveryRegistration.getCompId());
                        iCommonCompanyIdentityService.insert(commonCompanyIdentity);
                    }
                }
                shippingInformation.setId(IdGenerator.generateUUID());
                shippingInformation.setCompId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
                shippingInformation.setGrainId(grainDeliveryRegistration.getId());
                shippingInformation.setShipperPhone(identityAuthenticationInfo.getCustomerPhone());
                shippingInformation.setNotCarNo(shippingInformation.getCarNo());
                shippingInformation.setCustomerNumberCard(identityAuthenticationInfo.getCustomerNumberCard());
            }
        }
        // 操作主表数据
        this.insert(grainDeliveryRegistration);
        shippingInformationService.insertBatch(shippingInformationList);
        return "ok";
    }

    /**
     * 送粮记录列表
     * @param commonId
     * @return
     */
    @Override
    public List<GrainDeliveryRegistration> selectGrainDeliveryRegistration(String commonId){
//        Map<String, Object> pageView = new HashMap<>();
//        pageView.put("startRecord", (grainDeliveryRegistration.getCurrentPage() - 1)
//                * grainDeliveryRegistration.getPageSize());
//        //用户id
//        pageView.put("commonId", grainDeliveryRegistration.getCommonId());
//        pageView.put("pageSize", grainDeliveryRegistration.getPageSize());
//        pageView.put("currentPage", grainDeliveryRegistration.getCurrentPage());
//        // 查询送粮记录总数
//        Integer dataCount = baseMapper.getCountByCondition(pageView);
//        List<GrainDeliveryRegistration> dataList = baseMapper.getListByCondition(pageView);
//        Page<GrainDeliveryRegistration> page = new Page<>();
//        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
//        page.setTotal(dataCount == null ? 0 : dataCount);
//        page.setCurrent(grainDeliveryRegistration.getCurrentPage());
//        page.setSize(grainDeliveryRegistration.getPageSize());
//        return page;
        //根据用户id查询送粮登记
        List<GrainDeliveryRegistration> grainDeliveryRegistrationList = this.selectList(new EntityWrapper<GrainDeliveryRegistration>()
            .eq("common_id",commonId)
            .eq("delete_flag","0")
            .orderBy("validity_date", false));
        if (!CollectionUtils.isEmpty(grainDeliveryRegistrationList)){
            for (GrainDeliveryRegistration grainDeliveryRegistration : grainDeliveryRegistrationList){
                //查询送货信息
                List<ShippingInformation> shippingInformations = shippingInformationService.selectList(new EntityWrapper<ShippingInformation>()
                        .eq("grain_id",grainDeliveryRegistration.getId())
                        .eq("delete_flag","0"));
                if (!CollectionUtils.isEmpty(shippingInformations)) {
                    grainDeliveryRegistration.setShippingInformationList(shippingInformations);
                    grainDeliveryRegistration.setCreateDate(shippingInformations.get(0).getCreateDate());
                }
            }
        }
        return grainDeliveryRegistrationList;
    }

    /**
     * 删除送粮记录
     * @param id
     */
    @Override
    public void deleteInfo(String id){
        //查询送粮登记信息
        GrainDeliveryRegistration grainDeliveryRegistration = this.selectById(id);
        //删除送粮登记信息
        this.deleteById(grainDeliveryRegistration.getId());
        //查询送货信息
        List<ShippingInformation> shippingInformations = shippingInformationService.selectList(new EntityWrapper<ShippingInformation>()
            .eq("grain_id",id));
        if (!CollectionUtils.isEmpty(shippingInformations)) {
            for (ShippingInformation shippingInformation : shippingInformations) {
                shippingInformationService.deleteById(shippingInformation.getId());
            }
        }
    }

    /**
     *
     * 登记客户下拉列表
     * @param grainDeliveryRegistration
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GrainDeliveryRegistration> grainDeliveryRegistrationCustomer(GrainDeliveryRegistration grainDeliveryRegistration){
        //根据仓库名查询送粮登记
        List<GrainDeliveryRegistration> grainDeliveryRegistrationList = this.selectList(new EntityWrapper<GrainDeliveryRegistration>()
            .eq("warehouse_name",grainDeliveryRegistration.getWarehouseName())
            .eq("delete_flag","0"));
        if(!CollectionUtils.isEmpty(grainDeliveryRegistrationList)){
            for(GrainDeliveryRegistration grainDeliveryRegistration1 : grainDeliveryRegistrationList){
                //根据送粮登记查询货主
                List<ShippingInformation> shippingInformationList = shippingInformationService.selectList(new EntityWrapper<ShippingInformation>()
                        .eq("grain_id",grainDeliveryRegistration1.getId())
                        .eq("delete_flag","0"));
                        grainDeliveryRegistration1.setShippingInformationList(shippingInformationList);
            }
        }
        return grainDeliveryRegistrationList;
    }
}
