package com.hzs.sale.ticket.controller.manage;

import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.sale.ticket.ext.SaBuyTicketExt;
import com.hzs.common.domain.sale.ticket.ext.SaTicketExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.sale.ticket.param.BuyTicketParam;
import com.hzs.sale.ticket.param.SaTicketParam;
import com.hzs.sale.ticket.service.ISaTicketService;
import com.hzs.sale.ticket.vo.SaTicketVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 门票后台控制类
 * @author: sui q
 * @time: 2025/1/10 14:08
 * @classname: SaTickteController
 * @package_name: com.hzs.sale.ticket.controller.manage
 * version 1.0.0
 */
@RestController
@RequestMapping("/manage/ticket")
public class SaTicketController extends BaseController {

    @Autowired
    private ISaTicketService saTicketService;

    /**
     * @description: 会员前台查询门票活动列表
     * @param: [acTicket]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @PostMapping("/query")
    public TableDataInfo querySaTick(@RequestBody SaTicketParam saTicketParam) {
        saTicketParam.setPkCountry(SecurityUtils.getPkCountry());
        saTicketParam.setSystemType(SecurityUtils.getSystemType());

        startPage();
        List<SaTicketExt> ticketExtList = saTicketService.querySaTicketByParam(saTicketParam);
        List<SaTicketVO> ticketVOList = packageTicketVO(ticketExtList);
        TableDataInfo dataTable = getDataTable(ticketExtList);
        dataTable.setRows(ticketVOList);
        return dataTable;
    }

    /**
     * @param ticketParamList 订单参数
     * @Description: 支付购买订单
     * @return: AjaxResult
     * @Author: sui q
     * @Date: 2022/8/27 16:47
     */
    @PostMapping("/update-ticket")
    public AjaxResult update(@RequestBody List<BuyTicketParam> ticketParamList) {
        for (BuyTicketParam buyTicketParam : ticketParamList) {
            if (buyTicketParam.getPkId() == null) {
                return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.REQUIRED_NOT_EMPTY));
            }
        }
        if (!saTicketService.updateSaTicketByParam(SecurityUtils.getUserId(), ticketParamList)) {
            return AjaxResult.error("改手机号已经购买过门票，请重新输入手机号！");
        }
        return AjaxResult.success();
    }


    @PostMapping("/export")
    public void homeExport(HttpServletResponse response, SaTicketParam saTicketParam) {
        saTicketParam.setPkCountry(SecurityUtils.getPkCountry());
        saTicketParam.setSystemType(SecurityUtils.getSystemType());
        List<SaTicketExt> ticketExtList = saTicketService.querySaTicketByParam(saTicketParam);
        List<SaTicketVO> ticketVOList = packageTicketVO(ticketExtList);
        ExcelUtil<SaTicketVO> util = new ExcelUtil<>(SaTicketVO.class);
        util.exportExcel(response, ticketVOList, "自助购票");
    }

    private static List<SaTicketVO> packageTicketVO(List<SaTicketExt> ticketExtList) {
        List<SaTicketVO> ticketVOList = new ArrayList<>();
        for (SaTicketExt saTicketExt : ticketExtList) {
            for (SaBuyTicketExt saBuyTicket : saTicketExt.getSaBuyTicketList()) {
                SaTicketVO saTicketVO = SaTicketVO.builder()
                        .actName(saTicketExt.getActName())
                        .memberName(saTicketExt.getMemberName())
                        .memberCode(saTicketExt.getMemberCode())
                        .actStartDate(saTicketExt.getActStartDate())
                        .actEndDate(saTicketExt.getActEndDate())
                        .orderCode(saTicketExt.getOrderCode())
                        .orderAmount(saTicketExt.getOrderAmount())
                        .price(saTicketExt.getPrice())
                        .quantity(saTicketExt.getQuantity())
                        .buyName(saBuyTicket.getBuyName())
                        .phone(saBuyTicket.getPhone())
                        .idCard(saBuyTicket.getIdCard())
                        .sex(saBuyTicket.getSex())
                        .clothSize(saBuyTicket.getClothSize())
                        .cohabitant(saBuyTicket.getCohabitant())
                        .emergencyPhone(saBuyTicket.getEmergencyPhone())
                        .vertexName(saBuyTicket.getVertexName())
                        .teamName(saBuyTicket.getTeamName())
                        .creationTime(saTicketExt.getCreationTime())
                        .build();
                if (saTicketVO.getSex() == ESex.UNKNOWN.getValue()) {
                    saTicketVO.setSexVal(ESex.UNKNOWN.getLabel());
                } else if (saTicketVO.getSex() == ESex.MAN.getValue()) {
                    saTicketVO.setSexVal(ESex.MAN.getLabel());
                } else {
                    saTicketVO.setSexVal(ESex.WOMAN.getLabel());
                }
                ticketVOList.add(saTicketVO);
            }
        }
        return ticketVOList;
    }
}
