package com.iamberry.wechat.handles.agentInfo;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.agentInfo.AgentConfig;
import com.iamberry.wechat.core.entity.agentInfo.AgentOrder;
import com.iamberry.wechat.core.entity.agentInfo.AgentOrderItem;
import com.iamberry.wechat.core.entity.agentInfo.AgentTooth;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.product.Product;
import com.iamberry.wechat.face.agentInfo.AgentInfoService;
import com.iamberry.wechat.face.agentInfo.AgentPurchaseOrderService;
import com.iamberry.wechat.face.cart.ProductInfoService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 采购&订单
 */
@Controller
@RequestMapping("/wechat/agentOrder")
public class AgentOrderHandler {
    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private AgentPurchaseOrderService agentPurchaseOrderService;

    /**
     * 获取采购信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/getProduce")
    public ResultMsg getProduce(HttpServletRequest request) {
        ResultMsg rm = new ResultMsg();
        //Member member =  WechatUtils.getUserBySession(request);
        //String agentOpenid = member.getUserOpenid();

        String agentOpenid = "o-icasz5a10CfmJ_s-hvW-Ltqwtc";

        AgentTooth agentTooth = new AgentTooth();
        agentTooth.setAgentOpenid(agentOpenid);
        agentTooth = agentInfoService.getAgentTooth(agentTooth);
        if (agentTooth == null) {
            rm.setStatus(false);
            rm.setMessage("未找到该用户!");
            rm.setResultCode("404");
            return rm;
        }

        Product product = new Product();
        List<Product> productList = productInfoService.listSelectProduct(product);

        for (Product pd : productList) {
            AgentConfig agentConfig = new AgentConfig();
            agentConfig.setColorId(pd.getColorId());
            agentConfig.setAgentId(agentTooth.getAgentId());
            agentConfig = agentInfoService.getAgentConfig(agentConfig);
            if (agentConfig != null && agentConfig.getConfigAmount() != null && agentConfig.getConfigAmount() != 0) {
                pd.setConfigAmount(agentConfig.getConfigAmount());
            } else {
                pd.setConfigAmount(pd.getProductDiscount());
            }
        }
        rm.setStatus(true);
        rm.setMessage("查询成功!");
        rm.setResultCode("200");
        rm.setData(productList);
        return rm;
    }

    /**
     * 支付前，调用方法
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/payBefore", method = RequestMethod.POST)
    public ResultMsg payBefore(HttpServletRequest request,AgentOrder agentOrder) throws Exception {
        ResultMsg msg = new ResultMsg();
        msg.setStatus(false);
        msg.setResultCode("400");

        //获取代理商信息
        Member member =  WechatUtils.getUserBySession(request);
        String agentOpenid = member.getUserOpenid();
        AgentTooth agentTooth = new AgentTooth();
        agentTooth.setAgentOpenid(agentOpenid);
        agentTooth = agentInfoService.getAgentTooth(agentTooth);
        if (agentTooth == null) {
            msg.setMessage("未找到该用户!");
            return msg;
        }

        //验证前台传过来的数据是否规范
        if(!isValiAgentOrder(agentOrder)){
            msg.setMessage("订单格式内容出错!");
            return msg;
        }

        //---------------生成订单 --- start -----------------------
        JSONArray jsonArray = JSONArray.fromObject(agentOrder.getAgentOrderItemJson());
        List<AgentOrderItem>  agentOrderItemList = (List) JSONArray.toCollection(jsonArray,AgentOrderItem.class);
        if(agentOrderItemList == null || agentOrderItemList.size() < 1){
            msg.setMessage("未选择产品!");
            return msg;
        }

        agentPurchaseOrderService.addAgentOrder(agentOrder,agentOrderItemList);
        //---------------生成订单 --- end -----------------------




//        OrderPayDto dto = new OrderPayDto();
        //回显用户支付过的地址信息
//        Order order = cartService.getOrderRecentAddress(member.getUserOpenid());
//        if(order!=null){
//            HashMap<String,String> map = new HashMap<String,String>();
//            //map.put("orderId", order.getSalesOrderid());
//            map.put("salesAddressName", order.getSalesAddressName());
//            map.put("salesAddressInfo", order.getSalesAddressInfo());
//            map.put("salesAddressTel", order.getSalesAddressTel());
//            map.put("salesAddressPostnum", order.getSalesAddressPostnum());
//            dto.setAddressInfo(map);
//        }
//
//        msg.setResultCode(ResultInfo.SUCCESSCODE);
//        msg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
//        msg.setData(dto);
//        msg.setStatus(true);

        return msg;
    }


    /**
     * 支付前的验证
     * @param agentOrder
     * @return
     */
    public boolean isValiAgentOrder(AgentOrder agentOrder){
        if(agentOrder.getAgentAddressInfo() == null || agentOrder.getAgentAddressInfo() == ""){
            return false;
        }
        if (agentOrder.getAgentAddressName() == null || agentOrder.getAgentAddressName() == ""){
            return false;
        }
        if (agentOrder.getAgentAddressPostnum() == null || agentOrder.getAgentAddressPostnum() == ""){
            return false;
        }
        if (agentOrder.getAgentAddressTel() == null){
            return false;
        }
        if(agentOrder.getAgentOrderItemJson() == null || agentOrder.getAgentOrderItemJson() == ""){
            return false;
        }
        return true;
    }


}
