package com.library.service.Impl;

import com.library.config.NameValue;
import com.library.mapper.CommMapper;
import com.library.mapper.CurrencyMapper;

import com.library.model.AdminModel;
import com.library.model.UserModel;
import com.library.respcode.ServerResponse;
import com.library.service.AutomaticCodeService;
import com.library.service.CommService;

import com.library.util.MSMmethodUtil;
import com.library.util.Paygo24Utils;
import com.library.util.TransformationTools;
import com.wechat.pay.MD5Util;
import com.wechat.pay.WeChatManager;
import com.wechat.pay.WeChatManager.UserInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommServiceImpl implements CommService {

    @Resource
    private HttpServletRequest request;
    @Resource
    private CommMapper commMapper;
    @Resource
    private CurrencyMapper currencyMapper;

    @Resource
    private AutomaticCodeService automaticCodeService;

    @Override
    public ServerResponse Recharge() {
        try {

            return ServerResponse.createBySuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }

    @Override
    public ServerResponse adminLogin() {
        try {

            String phone = TransformationTools.TransactSQLInjection(request.getParameter("account"));//手机号码(type=1,2)
            String password = TransformationTools.TransactSQLInjection(request.getParameter("password"));//密码(type=2)

//            if (phone.length() < 6) {
//                return ServerResponse.createByErrorMessage("账户或者密码错误!");
//            }
            String new_password = MD5Util.MD5Encode(MD5Util.MD5Encode(password, null), null);

            List<Map<String, Object>> list = currencyMapper.FindDataListWhere(NameValue.Table_admin, 0, 1,
                    " where account = '" + phone + "' and password ='" + new_password + "'");
            if (list != null && list.size() > 0) {//登陆账户
                AdminModel adminModel = (AdminModel) TransformationTools.ToObjectOne(UserModel.class, list.get(0));
                request.getSession().setAttribute(NameValue.Admin_Session, adminModel);
                return ServerResponse.createBySuccess();
            } else {//账户或者密码错误
                return ServerResponse.createByErrorMessage("账户或者密码错误!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }


}
