package com.wgb.system.service.service;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.tool.data.external.domain.response.WechatPayUnifiedorderResponse;
import com.wgb.system.service.domain.model.Business;
import com.wgb.system.service.domain.model.SysUser;
import com.wgb.system.service.domain.model.User;
import com.wgb.system.service.domain.model.UserTask;
import com.wgb.system.service.domain.model.UserEnterprise;
import com.wgb.system.service.component.Md5Utls;
import com.wgb.system.service.exception.InternalSystemUnHandlerException;

import java.time.LocalDateTime;

public class ServiceBase {

    public void handlerBusines(Business business) {
        if (business == null) {
            throw new InternalSystemUnHandlerException("06");
        }
    }

    public void handlerUser(User user) {
        if (user == null) {
            throw new InternalSystemUnHandlerException("07");
        }
        if (user.getState() == 0) {
            throw new InternalSystemUnHandlerException("13");
        }
    }

    public void handlerSysUser(SysUser sysUser, String password) throws Exception {
        if (sysUser == null) {
            throw new InternalSystemUnHandlerException("03");
        }
        if (!Md5Utls.verify(password, sysUser.getPassword())) {
            throw new InternalSystemUnHandlerException("04");
        }
        if (sysUser.getState() == 0) {
            throw new InternalSystemUnHandlerException("13");
        }
    }

    public void handlerUserpsw(boolean result) {
        if (!result) {
            throw new InternalSystemUnHandlerException("04");
        }
    }

    public void handlerSms(SendSmsResponse response) {
        if (!response.getCode().equalsIgnoreCase("OK")) {
            throw new InternalSystemUnHandlerException("11");
        }
    }

    public void handlerSmsCode(String redisCode, String code) {
        if (redisCode == null) {
            throw new InternalSystemUnHandlerException("12");
        } else if (!code.equalsIgnoreCase(redisCode)) {
            throw new InternalSystemUnHandlerException("12");
        }
    }

    public void handlerUserTask(UserTask userTask, Long userId) {
        if (userTask != null && userTask.getUserId().equals(userId)) {
            throw new InternalSystemUnHandlerException("09");
        }
        if (userTask != null && LocalDateTime.now().minusMinutes(10).isBefore(userTask.getCreateTime())) {
            throw new InternalSystemUnHandlerException("08");
        }
    }

    public void handlerLicence(UserEnterprise enterprise) {
        if (enterprise != null && enterprise.getState().equals(1)) {
            throw new InternalSystemUnHandlerException("15");
        } else if (enterprise != null && enterprise.getState().equals(0)) {
            throw new InternalSystemUnHandlerException("14");
        }
    }

    public void handlerLicenceImage() {
        throw new InternalSystemUnHandlerException("16");
    }

    public void handlerImage() {
        throw new InternalSystemUnHandlerException("18");
    }

    public void handlerSendWechatBeforehandPay(WechatPayUnifiedorderResponse response) {
        if (!response.getReturn_code().equalsIgnoreCase("SUCCESS") || !response.getResult_code().equalsIgnoreCase("SUCCESS"))
            throw new InternalSystemUnHandlerException("17");
    }
}
