package com.order.controller.business;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.security.SessionObject;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsOrderCinema;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.qianzhu.CinemaService;
import com.hfkj.service.goods.BsOrderCinemaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value="/orderCinema")
@Api(value="电影票订单管理")
public class BsOrderCinemaController {
    private static final Logger log = LoggerFactory.getLogger(BsOrderCinemaController.class);

    @Resource
    private UserCenter userCenter;

    @Resource
    private BsOrderCinemaService bsOrderCinemaService;


    @RequestMapping(value="/getListCinema",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData getListCinema(
                                     @RequestParam(value = "orderNo" , required = false) String orderNo,
                                     @RequestParam(value = "childOrderNo" , required = false) String childOrderNo,
                                     @RequestParam(value = "userPhone" , required = false) String userPhone,
                                     @RequestParam(value = "goodsSpecsName" , required = false) String goodsSpecsName,
                                     @RequestParam(value = "goodsName" , required = false) String goodsName,
                                     @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<>();

            if (userModel.getAccount().getPlatformCode() != null) {
                map.put("platformCode", userModel.getAccount().getPlatformCode());
            }

            map.put("orderNo", orderNo);
            map.put("childOrderNo", childOrderNo);
            map.put("userPhone", userPhone);
            map.put("goodsSpecsName", goodsSpecsName);
            map.put("goodsName", goodsName);
            map.put("payType", payType);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("status", status);
            PageHelper.startPage(pageNum, pageSize);

            return ResponseMsgUtil.success(new PageInfo<>(bsOrderCinemaService.getList(map)));

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

    @RequestMapping(value="/queryOrder",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询订单")
    public ResponseData queryOrder(@RequestParam(value = "orderNo" , required = true) String orderNo) {
        try {

            BsOrderCinema bsOrderCinema = bsOrderCinemaService.findGoodsOrder(orderNo);

            Map<String , Object> map = new HashMap<>();
            map.put("orderNo" , bsOrderCinema.getChannelOrderNo());
            JSONObject object = CinemaService.getByOrderNo(map);

            if (object != null) {
                bsOrderCinema.setStatus(object.getInteger("status"));
                bsOrderCinema.setUpdateTime(new Date());
                bsOrderCinemaService.update(bsOrderCinema);
            }

            return ResponseMsgUtil.success(object);

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

}
