package com.kmall.api.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.kmall.api.annotation.IgnoreAuth;
import com.kmall.api.annotation.LoginUser;
import com.kmall.api.dto.IdCardMsgVo;
import com.kmall.api.dto.SendMsgVo;
import com.kmall.api.entity.*;
import com.kmall.api.service.*;
import com.kmall.api.util.ApiBaseAction;
import com.kmall.api.util.IdCardUtil;
import com.kmall.common.utils.RRException;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: @author Scott <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@RestController
@RequestMapping("/api/index")
public class ApiIndexController extends ApiBaseAction {
    protected Logger log = Logger.getLogger(ApiIndexController.class);
    @Autowired
    private ApiAdService apiAdService;
    @Autowired
    private ApiChannelService channelService;
//    @Autowired
//    private ApiGoodsService goodsService;
//    @Autowired
//    private ApiCartService cartService;
    @Autowired
    private ApiOrderService apiOrderService;
    @Autowired
    private ApiUserService apiUserService;
//    @Autowired
//    private ApiThirdMerchantBizService apiThirdMerchantBizService;
//    @Autowired
//    private ApiStoreService apiStoreService;

    /**
     * app首页
     */
    @IgnoreAuth
    @GetMapping("index")
    public Object index() {
        Map<String, Object> resultObj = Maps.newHashMap();
        // init
        Long userId = getUserId();
        Long storeId = getStoreId();
        //
        Map param = Maps.newHashMap();
//        param.put("ad_position_ids", new Integer[]{1, 2, 3});
        param.put("ad_position_ids", new Integer[]{1});//用户注册
        param.put("storeId",storeId);
        List<AdVo> adVos = apiAdService.queryList(param);
        //
        List<AdVo> banner = new ArrayList();
        if (null != adVos && adVos.size() > 0) {
            for (AdVo adVo : adVos) {
                if (adVo.getAdPositionId() == 1) {
                    banner.add(adVo);
                } else if (adVo.getAdPositionId() == 2) {
                    resultObj.put("groupBanner", adVo);
                }
            }
        }
        resultObj.put("banner", banner);
        //
        param = Maps.newHashMap();
        param.put("sidx", "sort_order ");
        param.put("order", "asc ");
        List<ChannelVo> channel = channelService.queryList(param);
        resultObj.put("channel", channel);

        return toResponsSuccess(resultObj);
    }

    /**
     * 个人中心主页
     */
    @GetMapping("ucenterIndex")
    public Object ucenterIndex() {
        Map resultObj = Maps.newHashMap();
        Long userId = getUserId();
        // 统计个人中心订单数据
        Map params = Maps.newHashMap();
        params.put("user_id", userId);
        Map countMap = apiOrderService.queryUcenterMap(params);
        resultObj.put("countMap", countMap);
        return toResponsSuccess(resultObj);
    }

    @PostMapping("idCardRealName")
    public Object idCardRealName(@LoginUser UserVo loginUser) {
        JSONObject jsonParam = getJsonRequest();

        Long userId = getUserId();
        String idNo = jsonParam.getString("idNo");
        String username = jsonParam.getString("userName");
        UserVo user = apiUserService.queryObjectByIdNoAndName(idNo, username);
        UserVo userVo = new UserVo();
        userVo.setUsername(username);
        userVo.setIdNo(idNo);
        userVo.setId(userId);
        try {
            if(user != null){
                if(loginUser.getId() != user.getId()){
                    return toResponsFail("该身份证信息已被绑定");
                }
            }
            //实名认证身份证信息
            String result = IdCardUtil.checkIdCard(idNo, username);
            logger.info("idcard check result:"+result);
            IdCardMsgVo vo = JSON.parseObject(result,IdCardMsgVo.class);
            if(vo.getRespCode().equalsIgnoreCase("0000")){
                apiUserService.update(userVo);
                return toResponsMsgSuccess("实名绑定成功");
            }else{
                return toResponsFail(vo.getRespMessage());
            }
        }catch (Exception e){
            e.printStackTrace();
            return toResponsFail("系统异常");
        }
    }
    public static void main(String[] args) {

    }

}
