package com.liquidnet.client.admin.zhengzai.kylin.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.liquidnet.client.admin.common.exception.BusinessException;
import com.liquidnet.client.admin.common.utils.StringUtils;
import com.liquidnet.client.admin.zhengzai.kylin.dto.OrderOutLineVo;
import com.liquidnet.client.admin.zhengzai.kylin.service.IImportService;
import com.liquidnet.commons.lang.util.HttpUtil;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.adam.dto.vo.AdamEntersVo;
import com.liquidnet.service.adam.dto.vo.AdamUserInfoVo;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.kylin.dto.vo.partner.KylinPerformanceMisVo;
import com.liquidnet.service.kylin.service.admin.IKylinPerformancesAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.List;

@Service
public class ImportServiceImpl implements IImportService {

    @Autowired
    IKylinPerformancesAdminService kylinPerformancesService;
    @Value("${liquidnet.service.adam.url}")
    private String adamUrl;

    /**
     * 导入订单数据
     */
    public String importOrderOutLine(String performancesId, List<OrderOutLineVo> orderOutLineVoList, Boolean updateSupport, String operName) {
        if (StringUtils.isNull(orderOutLineVoList) || orderOutLineVoList.size() == 0) {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (OrderOutLineVo orderOutLineVo : orderOutLineVoList) {

            //获取演出维度数据
            KylinPerformanceMisVo misVo = kylinPerformancesService.performanceDetails(performancesId);
            //获取账号维度数据
            String enterMobile = orderOutLineVo.getEnterMobile();
            String enterIdCode = orderOutLineVo.getEnterIdCode();
            String enterName = orderOutLineVo.getEnterName();
            String number = orderOutLineVo.getNumber();
            MultiValueMap<String, String> params = new LinkedMultiValueMap();
            params.add("mobile", orderOutLineVo.getEnterMobile());
            String returnData = HttpUtil.post(adamUrl.concat("/adam/rsc/reg/mobile"), params);
            AdamUserInfoVo adamUserInfoVo = JsonUtils.fromJson(returnData,new TypeReference<ResponseDto<AdamUserInfoVo>>() {}).getData();
            String uid =adamUserInfoVo.getUid();
            Boolean isMember;
            if(adamUserInfoVo.getStageMarker().equals("10") || adamUserInfoVo.getStageMarker().equals("11")){
                isMember = true;
            }else{
                isMember = false;
            }
            Boolean isTrueName;
            Boolean isStudent;
            String ticketTitle = orderOutLineVo.getTicketTitle();
            String useTime = orderOutLineVo.getUseTime();
            String ticketType ;
            if(orderOutLineVo.getTicketType().equals("电子票")){
                ticketType="electronic";
            }
//            else if(orderOutLineVo.getTicketType().equals("快递票")){
//                ticketType="express";
//            }


//            try {
//                // 验证是否存在这个用户
//                OrderOutLineVo u = studentMapper.selectStudentByName(student.getStudentName());
//                if (StringUtils.isNull(u)) {
//                    student.setStudentName(student.getStudentName());
//                    this.insertStudent(student);
//                    successNum++;
//                    successMsg.append("<br/>" + successNum + "学校信息" + student.getStudentName() + " 导入成功");
//                } else if (updateSupport) {
//                    student.setUpdateBy(operName);
//                    this.updateStudent(student);
//                    successNum++;
//                    successMsg.append("<br/>" + successNum + "学校信息 " + student.getStudentName() + " 更新成功");
//                } else {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "学校信息" + student.getStudentName() + " 已存在");
//                }
//            } catch (Exception e) {
//                failureNum++;
//                String msg = "<br/>" + failureNum + "学校信息" + student.getStudentName() + " 导入失败：";
//                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
//            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
