package cn.pluss.platform.deviceSignUnbind.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.device.DeviceService;
import cn.pluss.platform.deviceMerchantBuy.DeviceMerchantBuyService;
import cn.pluss.platform.deviceSignUnbind.DeviceSignUnbindService;
import cn.pluss.platform.entity.Device;
import cn.pluss.platform.entity.DeviceMerchantBuy;
import cn.pluss.platform.entity.DeviceSignUnbind;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantDevice;
import cn.pluss.platform.mapper.DeviceSignUnbindMapper;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantDevice.MerchantDeviceService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.StringUtil;

@Transactional
@RequiredArgsConstructor
@Slf4j
@Service("deviceSignUnbindService")
public class DeviceSignUnbindServiceImpl extends ServiceImpl<DeviceSignUnbindMapper, DeviceSignUnbind> implements DeviceSignUnbindService {


    private final DeviceMerchantBuyService deviceMerchantBuyService;

    private final DeviceService deviceService;

    private final UserAppService userAppService;

    private final MerchantDeviceService merchantDeviceService;

    private final MerchantBaseInfoService merchantBaseInfoService;

    @Override
    public DeviceSignUnbind queryDeviceSignUnbind(DeviceSignUnbind deviceSignUnbind) {
        return baseMapper.queryDeviceSignUnbind(deviceSignUnbind);
    }

    @Override
    public List<DeviceSignUnbind> queryDeviceSignUnbindPage(Map map) {
        return baseMapper.queryDeviceSignUnbindPage(map);
    }

    @Override
    public Integer queryDeviceSignUnbindPageCount(Map map) {
        return baseMapper.queryDeviceSignUnbindPageCount(map);
    }

    @Override
    public Result<String> applyDeviceSignUnbind(String orderNo, String soleCode, String sn) {
        DeviceMerchantBuy deviceMerchantBuy = new DeviceMerchantBuy();
        deviceMerchantBuy.setOrderNo(orderNo);
        deviceMerchantBuy.setStatus("1");
        List<DeviceMerchantBuy> queryDeviceMerchantBuyList = deviceMerchantBuyService.queryDeviceMerchantBuyList(deviceMerchantBuy);
        if (queryDeviceMerchantBuyList == null || queryDeviceMerchantBuyList.size() == 0) {
            return ResultGenerator.genFailResult("该订单未付款");
        }
        deviceMerchantBuy = queryDeviceMerchantBuyList.get(0);
        Device device = new Device();
        device.setVenderModel(deviceMerchantBuy.getVenderModel());
        List<Device> queryDeviceList = deviceService.queryDeviceList(device);
        if (queryDeviceList != null && queryDeviceList.size() > 0) {
            device = queryDeviceList.get(0);
        }
        DeviceSignUnbind deviceSignUnbind = new DeviceSignUnbind();
        deviceSignUnbind.setOrderNo(orderNo);
        deviceSignUnbind.setSoleCode(soleCode);
        List<DeviceSignUnbind> queryDeviceSignUnbindList = baseMapper.queryDeviceSignUnbindList(deviceSignUnbind);
        if (StringUtil.isEmpty(soleCode)) {
            return ResultGenerator.genFailResult("soleCode不能为空");
        }
        if (StringUtil.isEmpty(orderNo)) {
            return ResultGenerator.genFailResult("订单号不能为空");
        }
        if (queryDeviceSignUnbindList != null && queryDeviceSignUnbindList.size() > 0) {
            deviceSignUnbind = queryDeviceSignUnbindList.get(0);
            if ("2".equals(deviceSignUnbind.getStatus()) || "6".equals(deviceSignUnbind.getStatus())) {
                deviceSignUnbind.setStatus("3");
                baseMapper.updateDeviceSignUnbind(deviceSignUnbind);
                return ResultGenerator.genSuccessResult("申请解绑中，请耐心等待");
            } else if ("1".equals(deviceSignUnbind.getStatus()) || "4".equals(deviceSignUnbind.getStatus())) {
                deviceSignUnbind.setStatus("1");
                baseMapper.updateDeviceSignUnbind(deviceSignUnbind);
                return ResultGenerator.genFailResult("申请激活中，请耐心等待");
            } else if ("5".equals(deviceSignUnbind.getStatus())) {
                if (StringUtil.isEmpty(sn)) {
                    return ResultGenerator.genFailResult("请输入sn码");
                }
                deviceSignUnbind.setStatus("1");
                baseMapper.updateDeviceSignUnbind(deviceSignUnbind);
                return ResultGenerator.genSuccessResult("申请激活中，请耐心等待");
            } else {
                return ResultGenerator.genFailResult("申请解绑失败");
            }
        } else {
            if (StringUtil.isEmpty(sn)) {
                return ResultGenerator.genFailResult("请输入sn码");
            }
//            UserApp userApp = userAppService.queryUserAppByToken();
//            if (StringUtil.isEmpty(userApp.getLeshuaMchId())) {
//                return ResultGenerator.genFailResult("未开通乐刷支付");
//            }
//            deviceSignUnbind.setMchId(userApp.getLeshuaMchId());
            deviceSignUnbind.setSn(sn);
            deviceSignUnbind.setVenderCode(device.getVenderCode());
            deviceSignUnbind.setStatus("1");
            deviceSignUnbind.setVenderModel(device.getVenderModel());
            deviceSignUnbind.setMerchantName(deviceMerchantBuy.getMerchantName());
            deviceSignUnbind.setMerchantCode(deviceMerchantBuy.getMerchantCode());
            log.error(JSONObject.toJSONString(deviceSignUnbind));
            baseMapper.insert(deviceSignUnbind);
            MerchantBaseInfo merchantBaseInfo = new MerchantBaseInfo();
            merchantBaseInfo.setMerchantCode(deviceMerchantBuy.getMerchantCode());
            List<MerchantBaseInfo> queryMerchantBaseInfoList = merchantBaseInfoService.queryMerchantBaseInfoList(merchantBaseInfo);
            if (queryMerchantBaseInfoList != null && queryMerchantBaseInfoList.size() > 0) {
                merchantBaseInfo = queryMerchantBaseInfoList.get(0);
                MerchantDevice merchantDevice = new MerchantDevice();
                merchantDevice.setOrderNo(orderNo);
                merchantDevice.setSn(sn);
                merchantDevice.setVenderCode(device.getVenderCode());
                merchantDevice.setMerchantCode(deviceMerchantBuy.getMerchantCode());
                merchantDevice.setMerchantName(deviceMerchantBuy.getMerchantName());
                merchantDevice.setUserId(Long.parseLong(merchantBaseInfo.getUserId()));
                merchantDevice.setStatus(0);
                merchantDevice.setCreateDt(new Date());
                merchantDevice.setVenderModel(device.getVenderModel());
                //添加商家设备
                merchantDeviceService.save(merchantDevice);
            }
            return ResultGenerator.genSuccessResult("申请激活中,请耐心等待");
        }
    }


}

