package com.order.controller.business;

import com.github.pagehelper.PageInfo;

import com.hfkj.common.security.SessionObject;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.PageUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsOrderChild;
import com.hfkj.entity.BsOrderStarbucks;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.model.order.OrderStarbucksModel;
import com.hfkj.service.goods.BsOrderStarbucksService;
import com.hfkj.service.order.BsOrderChildService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value="/orderStarbucks")
@Api(value="星巴克订单管理")
public class BsOrderStarbucksController {

    private static final Logger log = LoggerFactory.getLogger(BsOrderStarbucksController.class);

    @Resource
    private BsOrderChildService bsOrderChildService;

    @Resource
    private BsOrderStarbucksService bsOrderStarbucksService;

    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/getListStarbucks",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData getListStarbucks(
            @RequestParam(value = "orderNo" , required = false) String orderNo,
            @RequestParam(value = "childOrderNo" , required = false) String childOrderNo,
            @RequestParam(value = "userPhone" , required = false) String userPhone,
            @RequestParam(value = "productSpecName" , required = false) String productSpecName,
            @RequestParam(value = "storeAddress" , required = false) String storeAddress,
            @RequestParam(value = "storeName" , required = false) String storeName,
            @RequestParam(value = "productName" , required = false) String productName,
            @RequestParam(value = "payType" , required = false) Integer payType,
            @RequestParam(value = "status" , required = false) Integer status,
            @RequestParam(value = "createTimeS" , required = false) Long createTimeS,
            @RequestParam(value = "createTimeE" , required = false) Long createTimeE,
            @RequestParam(value = "pageNum" , required = true) Integer pageNum,
            @RequestParam(value = "pageSize" , required = true) Integer pageSize, HttpServletRequest request) {
        try {


            SessionObject sessionObject = userCenter.getSessionObject(request);
            SecUserSessionObject userModel = (SecUserSessionObject) sessionObject.getObject();

            Map<String , Object> map = new HashMap<>();


            map.put("orderNo", orderNo);
            map.put("childOrderNo", childOrderNo);
            map.put("productType", 4);
            map.put("productSpecName", productSpecName);
            map.put("productName", productName);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("status", status);

            List<BsOrderChild> list = bsOrderChildService.getList(map);

            Map<String , Object> map1 = new HashMap<>();

            if (userModel.getAccount().getPlatformCode() != null) {
                map1.put("platformCode", userModel.getAccount().getPlatformCode());
            }
            List<BsOrderStarbucks> starbucksList = bsOrderStarbucksService.getList(map1);

            List<OrderStarbucksModel> starbucksModelList = new ArrayList<>();

            for (BsOrderChild bsOrderChild : list) {
                OrderStarbucksModel starbucksModel = new OrderStarbucksModel();
                BeanUtils.copyProperties(bsOrderChild, starbucksModel);

                List<BsOrderStarbucks> starbucks= starbucksList.stream().filter(s -> s.getOrderNo().equals(starbucksModel.getOrderNo())).
                        collect(Collectors.toList());
                if (!starbucks.isEmpty()) {
                    starbucksModel.setStoreAddress(starbucks.get(0).getStoreAddress());
                    starbucksModel.setStoreName(starbucks.get(0).getStoreName());
                    starbucksModel.setUserPhone(starbucks.get(0).getUserPhone());
                    starbucksModel.setPayType(starbucks.get(0).getPayType());
                    starbucksModelList.add(starbucksModel);
                }
            }

            if (storeAddress != null) {
                starbucksModelList = starbucksModelList.stream().filter(s -> s.getStoreAddress().contains(storeAddress)) // 使用contains方法进行模糊查询
                        .collect(Collectors.toList());
            }
            if (storeName != null) {
                starbucksModelList = starbucksModelList.stream().filter(s -> s.getStoreName().contains(storeName)) // 使用contains方法进行模糊查询
                        .collect(Collectors.toList());
            }
            if (userPhone != null) {
                starbucksModelList = starbucksModelList.stream().filter(s -> s.getUserPhone().contains(userPhone)) // 使用contains方法进行模糊查询
                        .collect(Collectors.toList());
            }

            return ResponseMsgUtil.success(PageUtil.initPageInfoObj(pageNum, starbucksModelList.size(), pageSize, new PageInfo<>(starbucksModelList)));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }
}
