package org.linlinjava.jz.admin.web;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.sun.istack.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.jz.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.jz.core.util.ResponseUtil;
import org.linlinjava.jz.core.validator.Order;
import org.linlinjava.jz.core.validator.Sort;
import org.linlinjava.jz.db.domain.CustomerClient;
import org.linlinjava.jz.db.domain.JzOrder;
import org.linlinjava.jz.db.service.CustomerClientService;
import org.linlinjava.jz.db.service.JzOrderService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "Admin-Order-Controller", description = "订单管理")
@RestController
@RequestMapping("/admin/order")
@Validated
public class AdminOrderController {

    private final Log logger = LogFactory.getLog(AdminOrderController.class);

    @Resource
    private JzOrderService jzOrderService;
    @Resource
    private CustomerClientService customerClientService;

    /**
     * 查询订单
     *
     * @param mobile
     * @param orderSn
     * @param orderStatusArray
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */

    @ApiOperation(value = "订单列表查询")
    @RequiresPermissions("admin:order:list")
    @RequiresPermissionsDesc(menu = {"订单中心", "订单管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(required = false) String mobile, @RequestParam(required = false) String orderSn,
                       @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
                       @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end,
                       @RequestParam(required = false) List<Short> orderStatusArray,
                       @RequestParam(required = false) Integer orderChannel,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<JzOrder> jzOrders = jzOrderService.querySelective(mobile, orderSn, start, end, orderStatusArray,orderChannel, page, limit, sort, order);
        return ResponseUtil.okList(jzOrders);
    }

    /**
     * 根据用户id同步订单数据
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户id同步订单数据")
    @GetMapping("/syncOrderByUserId")
    public Object syncOrderByUserId(@NotNull @RequestParam Integer userId) {
        CustomerClient customerClient = customerClientService.findById(userId);
        if (null == customerClient) {
            return ResponseUtil.badArgumentValue();
        }

        logger.info("同步盒马服务单");

        HttpStatus httpStatus = jzOrderService.syncHmBookOrderByPhone(customerClient.getTel());
        if (HttpStatus.OK == httpStatus) {
            logger.info("同步盒马服务付款单");
            HttpStatus httpStatus1 = jzOrderService.syncHmPaymentOrderByPhone(customerClient.getTel());
            if (HttpStatus.OK == httpStatus1) {
                Map<Object, Object> map = new HashMap<>(16);
                List<JzOrder> jzOrders = jzOrderService.findByUserId(userId);
                map.put("jzOrders", jzOrders);
                return ResponseUtil.ok(map);
            }
        }
        return ResponseUtil.fail();
    }

}
