package com.prolog.cs.biz.outbound.service.impl;

import com.prolog.cs.biz.outbound.dao.FreightEnterMapper;
import com.prolog.cs.biz.outbound.model.dto.FreightEnterDto;
import com.prolog.cs.biz.outbound.model.entity.FreightEnter;
import com.prolog.cs.biz.outbound.model.vo.FreightEnterVo;
import com.prolog.cs.biz.outbound.service.FreightEnterService;
import com.prolog.framework.authority.core.dto.UserDTO;
import com.prolog.framework.authority.user.UserTemplate;
import com.prolog.framework.utils.AssertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cheng.kong
 * @date 2022/8/3   17:21
 */
@Service
public class FreightEnterServiceImpl implements FreightEnterService {

    @Resource
    private FreightEnterMapper freightEnterMapper;

    @Autowired
    private UserTemplate userTemplate;

    @Override
    public List<FreightEnterDto> queryFreightEnter(FreightEnterVo vo) {
        UserDTO userDTO = userTemplate.getUserDTO();
        vo.setEnterpriseId(userDTO.getEnterpriseId());
        List<FreightEnterDto> list = freightEnterMapper.queryFreightEnter(vo);
//        List<String> shipNos = list.stream().map(FreightEnterDto::getShipNo).distinct().collect(Collectors.toList());

        list.forEach(row->{
            if(!row.getShipNo().isEmpty() && row.getShipNo().length()>4){
                String shipNo = row.getShipNo();
                String itemNo = shipNo.substring(shipNo.length() - 4);
                row.setItemNo(itemNo);
            }
        });

        return list;
    }

    @Override
    public Boolean saveFreightEnter(List<FreightEnterDto> vos) {
        UserDTO userDTO = userTemplate.getUserDTO();
        List<FreightEnter> list = new ArrayList<>();
        vos.forEach(row->{
            AssertUtils.isNotBlank(row.getFreightPrice().toString(), "运费必填");
            AssertUtils.isNotNull(row.getVisitPrice(), "上门费必填");
            AssertUtils.isNotNull(row.getFreightWayCode(), "付运费方式必填");

            FreightEnter freightEnter = new FreightEnter();
            freightEnter.setEnterpriseId(userDTO.getEnterpriseId());
            freightEnter.setWarehouseId(row.getWarehouseId());
            freightEnter.setWarehouseCode(row.getWarehouseCode());
            freightEnter.setWarehouseName(row.getWarehouseName());
            freightEnter.setShipNo(row.getShipNo());
            freightEnter.setItemNo(row.getItemNo());
            freightEnter.setCustomerCode(row.getCustomerCode());
            freightEnter.setCustomerName(row.getCustomerName());
            freightEnter.setContactWay(row.getContactWay());
            freightEnter.setAgencyFund(row.getAgencyFund());
            freightEnter.setFreightPrice(row.getFreightPrice());
            freightEnter.setVisitPrice(row.getVisitPrice());
            freightEnter.setBoxNumber(row.getBoxNumber());
            freightEnter.setFreightWayCode(row.getFreightWayCode());
            freightEnter.setFreightWayName(row.getFreightWayName());
            freightEnter.setStatus("0");
            freightEnter.setCreateBy(userDTO.getNickname());
            freightEnter.setCreateDate(new Date());
            freightEnter.setLastModifyBy(userDTO.getNickname());
            freightEnter.setLastModifyDate(new Date());
            list.add(freightEnter);
        });
        freightEnterMapper.saveBatch(list);
        return true;
    }
}
