package com.kmall.api.api;

import com.alibaba.fastjson.JSONObject;
import com.kmall.api.annotation.IgnoreAuth;
import com.kmall.api.annotation.LoginUser;
import com.kmall.api.entity.*;
import com.kmall.api.service.*;
import com.kmall.api.util.ApiBaseAction;
import com.kmall.common.utils.RRException;
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 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;

    /**
     * app首页
     */
    @IgnoreAuth
    @GetMapping("index")
    public Object index() {
        Map<String, Object> resultObj = new HashMap();
        // init
        Long userId = getUserId();
        Long storeId = getStoreId();
        //
        Map param = new HashMap();
//        param.put("ad_position_ids", new Integer[]{1, 2, 3});
        param.put("ad_position_ids", new Integer[]{1});//用户注册
        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 = new HashMap();
        param.put("sidx", "sort_order ");
        param.put("order", "asc ");
        List<ChannelVo> channel = channelService.queryList(param);
        resultObj.put("channel", channel);
        // 超级折扣，需要门店过滤
        param = new HashMap();
        param.put("fields", "distinct a.id,a.name,a.goods_brief,a.list_pic_url,psr1.retail_price,psr1.market_price,b.id as product_id,psr1.stock_num");
        param.put("is_hot", "1");
        param.put("offset", 0);
        param.put("limit", 3);
        param.put("is_delete", 0);
        param.put("store_id", storeId);
        List<GoodsVo> hotGoods = goodsService.queryHotGoodsList(param);
        resultObj.put("hotGoodsList", hotGoods);
        // 当前购物车中
        List<CartVo> cartList = new ArrayList();
        if (null != userId) {
            //查询列表数据
            Map cartParam = new HashMap();
            cartParam.put("user_id", userId);
            cartParam.put("store_id", storeId);
            cartList = cartService.queryList(cartParam);
        }
        if (null != cartList && cartList.size() > 0 && null != hotGoods && hotGoods.size() > 0) {
            for (GoodsVo goodsVo : hotGoods) {
                for (CartVo cartVo : cartList) {
                    if (goodsVo.getId().equals(cartVo.getGoods_id())) {
                        goodsVo.setCart_num(cartVo.getNumber());
                    }
                }
            }
        }
        return toResponsSuccess(resultObj);
    }

    /**
     * 个人中心主页，是否是骑手
     */
    @GetMapping("ucenterIndex")
    public Object ucenterIndex() {
        Map resultObj = new HashMap();
        Long userId = getUserId();
        // 统计个人中心订单数据
        Map params = new HashMap();
        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()){
                    // TODO: 2018/10/18 实名认证身份证信息
                    apiUserService.update(userVo);
                    return toResponsMsgSuccess("实名绑定成功");
                }else{
                    return toResponsFail("该身份证信息已被绑定");
                }
            }else{
                // TODO: 2018/10/18 实名认证身份证信息
                apiUserService.update(userVo);
                return toResponsMsgSuccess("实名绑定成功");

            }
        }catch (Exception e){
            log.error("系统异常",e);
            return toResponsFail("系统异常");
        }
    }
}