package com.fjs.scenic.controller.good;

import com.fjs.scenic.annotation.LoggerManage;
import com.fjs.scenic.controller.common.BaseController;
import com.fjs.scenic.entity.good.Goods;
import com.fjs.scenic.entity.order.ErrorOrder;
import com.fjs.scenic.entity.order.Orders;
import com.fjs.scenic.service.good.GoodsService;
import com.fjs.scenic.service.ord.ErrorOrderService;
import com.fjs.scenic.service.ord.OrdersService;
import com.fjs.scenic.service.ticket.TicketService;
import com.fjs.scenic.utils.Cons;
import com.fjs.scenic.utils.ReturnResult;

import com.fjs.scenic.utils.WXPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * This class is used for ...
 *
 * @author lfj
 * @version 1.0
 * @CreateDate ${date} ${time}
 */
@RestController
@RequestMapping("/good")
public class GoodsController extends BaseController<Goods> {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private ErrorOrderService errorOrderService;
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/index")
    public ModelAndView sceniclist(HttpSession session) {
        return new ModelAndView("/good/goods");
    }

    /**
     * 手动退款
     */
    @GetMapping("/refund")
    @ResponseBody
    @LoggerManage(logDescription = "手动退款")
    public ReturnResult refund(Integer orderId) {
        return ordersService.refundOrder(orderId);
    }

    /**
     * 手动退款 refundByOrderNo
     */
    @GetMapping("/refundByOrderNo")
    @ResponseBody
    @LoggerManage(logDescription = "手动退款")
    public ReturnResult refundByOrderNo(String orderNo) {
        Orders order = ordersService.selectByOrderNo(orderNo);
        if (order == null) {
            return ReturnResult.error("未查询到订单" + orderNo);
        } else {
            DecimalFormat decimalFormat = new DecimalFormat("#");
            ordersService.refundCytOrderByOrder(order);
            String b = WXPayUtil.refund(order.getOrderNo(), decimalFormat.format(order.getTotelPrice()));
            if ("true".equals(b)) {
                return ReturnResult.ok();
            } else if ("订单已全额退款".equals(b)) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                ordersService.updateByParam(" status=7,refund_time='" + format.format(new Date()) + "',refund_price='" + order.getTotelPrice() + "' where order_no='" + orderNo + "'");
                //TODO 删除异常订单
                ErrorOrder record = new ErrorOrder();
                record.setOrderNo(orderNo);
                errorOrderService.delete(record);
                return ReturnResult.ok();
            } else {
                return ReturnResult.error("掉微信退款失败");
            }
        }
    }

    /**
     * 手动出票
     */
    @GetMapping("/outTicket")
    @ResponseBody
    @LoggerManage(logDescription = "手动出票")
    public ReturnResult outTicket(Integer orderId) {
        return ordersService.outTicket(orderId);
    }

    /**
     * 同步门票
     */
    @GetMapping("/synTicket")
    @ResponseBody
    @LoggerManage(logDescription = "同步门票")
    public void synTicket() {
        ticketService.getProduct();
    }

    /**
     * 暂停售票
     */
    @PostMapping("/stopSaleTicket")
    @ResponseBody
    @LoggerManage(logDescription = "暂停售票")
    public ReturnResult stopSaleTicket(Date startDate, Date endDate) {
        return ReturnResult.ok(goodsService.saleOrRecoverSaleTicket(startDate, endDate, Cons.SaleTicket_Type.Stop));
    }

    /**
     * 恢复售票
     */
    @PostMapping("/recoverSaleTicket")
    @ResponseBody
    @LoggerManage(logDescription = "恢复售票")
    public ReturnResult recoverSaleTicket(Date startDate, Date endDate) {
        return ReturnResult.ok(goodsService.saleOrRecoverSaleTicket(startDate, endDate, Cons.SaleTicket_Type.Recover));
    }

}
