package com.zehong.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.zehong.common.utils.DateUtils;
import com.zehong.common.utils.StringUtils;
import com.zehong.system.domain.TYehuaqiUser;
import com.zehong.system.domain.vo.TLpgDeliveryRecordImportVo;
import com.zehong.system.mapper.TYehuaqiUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zehong.system.mapper.TLpgDeliveryRecordMapper;
import com.zehong.system.domain.TLpgDeliveryRecord;
import com.zehong.system.service.ITLpgDeliveryRecordService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 液化石油配送记录Service业务层处理
 * 
 * @author zehong
 * @date 2024-07-22
 */
@Service
public class TLpgDeliveryRecordServiceImpl implements ITLpgDeliveryRecordService 
{
    @Resource
    private TLpgDeliveryRecordMapper tLpgDeliveryRecordMapper;

    @Resource
    private TYehuaqiUserMapper yehuaqiUserMapper;

    /**
     * 查询液化石油配送记录
     * 
     * @param deliveryRecordId 液化石油配送记录ID
     * @return 液化石油配送记录
     */
    @Override
    public TLpgDeliveryRecord selectTLpgDeliveryRecordById(Long deliveryRecordId)
    {
        return tLpgDeliveryRecordMapper.selectTLpgDeliveryRecordById(deliveryRecordId);
    }

    /**
     * 数据导入
     * @param XmbhList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param deptId 操作用户
     * @param response r
     * @return r
     */
    @Override
    public String importLpgDelivertyRecordInfo(List<TLpgDeliveryRecordImportVo> XmbhList, Boolean isUpdateSupport, String deptId, HttpServletResponse response) {

        for (TLpgDeliveryRecordImportVo tLpgDeliveryRecordImportVo : XmbhList) {
            System.out.println(tLpgDeliveryRecordImportVo.getNickName());
            System.out.println(tLpgDeliveryRecordImportVo.getDeliveryAddress());
        }

        System.out.println("deptId = " + deptId);

        Integer errorNum = 0;
        Integer successNum = 0;

        List<TLpgDeliveryRecord> successData = new ArrayList<>();

        TLpgDeliveryRecord lpgDeliveryRecord;
        for (TLpgDeliveryRecordImportVo tLpgDeliveryRecordImportVo : XmbhList) {
            lpgDeliveryRecord = new TLpgDeliveryRecord();

            // 如果都为空 则 返回
            if (StringUtils.isBlank(tLpgDeliveryRecordImportVo.getNickName())
            && StringUtils.isBlank(tLpgDeliveryRecordImportVo.getPhone())
            && StringUtils.isBlank(tLpgDeliveryRecordImportVo.getAddress())
            && StringUtils.isBlank(tLpgDeliveryRecordImportVo.getUserOwnId())) {
                errorNum ++;
                continue;
            }

            // 如果 用户名 + 电话 + 地址 其中一个为空，则 userOwnId 不许为空

            if((StringUtils.isBlank(tLpgDeliveryRecordImportVo.getNickName()) ||
                    StringUtils.isBlank(tLpgDeliveryRecordImportVo.getPhone()) ||
                    StringUtils.isBlank(tLpgDeliveryRecordImportVo.getAddress()))
             && StringUtils.isBlank(tLpgDeliveryRecordImportVo.getUserOwnId())) {
                errorNum ++;
                continue;
            }


            if (StringUtils.isNotBlank(tLpgDeliveryRecordImportVo.getUserOwnId())) {

                TYehuaqiUser yehuaqiUser = yehuaqiUserMapper.selectByUserOwnIdAndBeyondEnterpriseId(deptId, tLpgDeliveryRecordImportVo.getUserOwnId());

                if (yehuaqiUser == null) {
                    List<TYehuaqiUser> yehuaqiUsers = yehuaqiUserMapper.selectByUsernameAndPhoneAndAddress(tLpgDeliveryRecordImportVo.getNickName(),
                            tLpgDeliveryRecordImportVo.getPhone(), tLpgDeliveryRecordImportVo.getAddress());
                    if (yehuaqiUsers == null || yehuaqiUsers.size() == 0) {
                        errorNum ++;
                        continue;
                    } else {
                        lpgDeliveryRecord.setUserId(yehuaqiUsers.get(0).getUserId());
                    }
                } else {
                    lpgDeliveryRecord.setUserId(yehuaqiUser.getUserId());
                }
            }
        }

        return null;
    }

    /**
     * 查询液化石油配送记录列表
     * 
     * @param tLpgDeliveryRecord 液化石油配送记录
     * @return 液化石油配送记录
     */
    @Override
    public List<TLpgDeliveryRecord> selectTLpgDeliveryRecordList(TLpgDeliveryRecord tLpgDeliveryRecord)
    {
        return tLpgDeliveryRecordMapper.selectTLpgDeliveryRecordList(tLpgDeliveryRecord);
    }

    /**
     * 新增液化石油配送记录
     * 
     * @param tLpgDeliveryRecord 液化石油配送记录
     * @return 结果
     */
    @Override
    public int insertTLpgDeliveryRecord(TLpgDeliveryRecord tLpgDeliveryRecord)
    {
        tLpgDeliveryRecord.setCreateTime(DateUtils.getNowDate());
        return tLpgDeliveryRecordMapper.insertTLpgDeliveryRecord(tLpgDeliveryRecord);
    }

    /**
     * 修改液化石油配送记录
     * 
     * @param tLpgDeliveryRecord 液化石油配送记录
     * @return 结果
     */
    @Override
    public int updateTLpgDeliveryRecord(TLpgDeliveryRecord tLpgDeliveryRecord)
    {
        tLpgDeliveryRecord.setUpdateTime(DateUtils.getNowDate());
        return tLpgDeliveryRecordMapper.updateTLpgDeliveryRecord(tLpgDeliveryRecord);
    }

    /**
     * 批量删除液化石油配送记录
     * 
     * @param deliveryRecordIds 需要删除的液化石油配送记录ID
     * @return 结果
     */
    @Override
    public int deleteTLpgDeliveryRecordByIds(Long[] deliveryRecordIds)
    {
        return tLpgDeliveryRecordMapper.deleteTLpgDeliveryRecordByIds(deliveryRecordIds);
    }

    /**
     * 删除液化石油配送记录信息
     * 
     * @param deliveryRecordId 液化石油配送记录ID
     * @return 结果
     */
    @Override
    public int deleteTLpgDeliveryRecordById(Long deliveryRecordId)
    {
        return tLpgDeliveryRecordMapper.deleteTLpgDeliveryRecordById(deliveryRecordId);
    }
}
