package com.winsun.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.bean.YxtAddress;
import com.winsun.bean.YxtCoupon;
import com.winsun.mapper.YxtAddressMapper;
import com.winsun.mapper.YxtCouponMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 兑换券校检
 *
 * @Author: xiangli
 * @Date:
 */
@Slf4j
@RestController
@RequestMapping("/yxtCard")
public class YxtCardController extends BaseController {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private YxtCouponMapper yxtCouponMapper;
    @Autowired
    private YxtAddressMapper yxtAddressMapper;

    /**
     * 进入院线通兑换券销售界面
     * @return
     */
    @RequestMapping("/getCardCount")
    public ResponseData<Map<String, Object>> getCardCount() {

        Wrapper<YxtCoupon> yxtCouponWrapper = new EntityWrapper<>();
        yxtCouponWrapper.eq("state",1);
        yxtCouponWrapper.eq("shelf_state",1);
        Integer cardNum = yxtCouponMapper.selectCount(yxtCouponWrapper);
        Wrapper<YxtCoupon> yxtCouponWrapper1 = new EntityWrapper<>();
        yxtCouponWrapper1.eq("state",2);
        Integer salesNum = yxtCouponMapper.selectCount(yxtCouponWrapper1);
        Map<String, Object> map = new Hashtable<>();
        map.put("cardNum",cardNum);
        map.put("salesNum",salesNum);
        map.put("nMoney",25.0);
        return ResponseData.success(map);
    }

    /**
     * 进入院线通实体票下单界面
     * @return
     */
    @RequestMapping("/enterPay")
    public ResponseData<String> enterPay(){
        /*String  code = request.getParameter("code");
        String result = WxInterfacesUtil.getAccess_tokenByCode(code);
        if (StringUtils.isNotBlank(result)) {
            JSONObject obj = JSONObject.fromObject(result);
            String openId = String.valueOf(obj.get("openid"));
            // code重复使用时，从session拿出第一次保存openId赋予openId
            if("null".equals(openId) && !"null".equals(String.valueOf(request.getSession().getAttribute("openId")))){
                openId = String.valueOf(request.getSession().getAttribute("openId"));
            }
            // code重复使用时，openId为"null"，不更新session
            if(!"null".equals(openId)){
                request.getSession().setAttribute("openId", openId);
            }
            request.setAttribute("openId", openId);
            System.out.println("openId:"  +openId);
        }*/
        String openId = "isOk";
        return ResponseData.success(openId);
    }
    /**
     * 获取用户地址
     * @return
     */
    @RequestMapping("/getUserAddress")
    public ResponseData<List<YxtAddress>> getUserAddress(@RequestParam("userId") String userId,@RequestParam("addressId") String addressId){
        Wrapper<YxtAddress> wrapper = new EntityWrapper<>();
        if(StringUtils.isNotBlank(addressId)){
            wrapper.eq(StringUtils.isNotBlank(userId),"id",addressId);
        }else{
            wrapper.eq("user_id",userId);
        }
        List<YxtAddress> list = yxtAddressMapper.selectList(wrapper);
        return ResponseData.success(list);
    }
    /**
     * 获取用户地址
     * @return
     */
    @RequestMapping("/addAddress")
    public ResponseData<List<YxtAddress>> addAddress(@RequestParam("address") String address){
        HashMap hashMap = JSON.parseObject(address, HashMap.class);
        YxtAddress yxtAddress = new YxtAddress();
        String userId = hashMap.get("userId").toString();
        yxtAddress.setUserId(userId);
        String consignee = hashMap.get("consignee").toString();
        if(StringUtils.isBlank(consignee)){
            return ResponseData.error("收货人不能为空！！！");
        }
        yxtAddress.setConsignee(consignee);
        String phone = hashMap.get("phone").toString();
        if(StringUtils.isBlank(phone)){
            return ResponseData.error("联系电话不能为空！！！");
        }
        yxtAddress.setPhone(phone);
        String area = hashMap.get("area").toString();
        if(StringUtils.isBlank(area)){
            return ResponseData.error("地址不能为空！！！");
        }
        yxtAddress.setArea(area);
        yxtAddress.setDetailAddress(hashMap.get("detailAddress").toString());
        yxtAddress.setUpdateDate(new Date());
        Integer num = yxtAddressMapper.insert(yxtAddress);
        if(num==1){
            return ResponseData.success();
        }
        return ResponseData.error("新增地址失败！！！");
    }


}
