package com.hzs.activity.ticket.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hzs.activity.ticket.service.IAcTicketService;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.annotation.RepeatSubmitSimple;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.StringUtils;
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.activity.ticket.AcTicket;
import com.hzs.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * 门票 前端控制器
 *
 * @author hzs
 * @since 2025-01-03
 */
@RestController
@RequestMapping("/manage/ticket")
public class AcTicketController extends BaseController {

    @Autowired
    private IAcTicketService acTicketService;

    /**
     * @description: 门票活动列表
     * @author: zhang jing
     * @date: 2025/1/3 16:27
     * @param: [acTicket]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.TICKET_GIVE, business = EOperationBusiness.TICKET_GIVE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(AcTicket acTicket) {
        startPage();
        LambdaQueryWrapper<AcTicket> acqe = new LambdaQueryWrapper<>();
        acqe.eq(AcTicket::getPkCountry, SecurityUtils.getPkCountry());
        acqe.eq(AcTicket::getSystemType, SecurityUtils.getSystemType());
        if (StringUtils.isNotEmpty(acTicket.getActName())) {
            acqe.like(AcTicket::getActName, acTicket.getActName());
        }
        if (StringUtils.isNotNull(acTicket.getActStartDate())) {
            acqe.ge(AcTicket::getActStartDate, acTicket.getActStartDate());
        }
        if (StringUtils.isNotNull(acTicket.getActEndDate())) {
            acqe.le(AcTicket::getActEndDate, acTicket.getActEndDate());
        }
        List<AcTicket> list = acTicketService.list(acqe);
        return getDataTable(list);
    }

    /**
     * @description: 新增门票活动
     * @author: zhang jing
     * @date: 2025/1/3 15:00
     * @param: [acTicket]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @RepeatSubmitSimple
    @PostMapping("/save")
    @Log(module = EOperationModule.TICKET_GIVE, business = EOperationBusiness.TICKET_GIVE, method = EOperationMethod.INSERT)
    public AjaxResult saveCuBonusExpand(@Valid @RequestBody AcTicket acTicket) {
        //校验活动是否已经添加
        LambdaQueryWrapper<AcTicket> acqe = new LambdaQueryWrapper<>();
        acqe.eq(AcTicket::getActStartDate, acTicket.getActStartDate());
        acqe.eq(AcTicket::getActEndDate, acTicket.getActEndDate());
        List<AcTicket> list = acTicketService.list(acqe);
        if (CollectionUtil.isNotEmpty(list)) {
            return AjaxResult.error(ConfigMsgConstants.ACTIVITY_REPEAT);
        }
        acTicket.setCreationTime(new Date());
        acTicket.setPkCreator(SecurityUtils.getUserId());
        acTicket.setPkCountry(SecurityUtils.getPkCountry());
        acTicket.setSystemType(SecurityUtils.getSystemType());
        return AjaxResult.success(acTicketService.save(acTicket));
    }

    /**
     * @description: 查询单条新零售门票活动信息
     * @author: zhang jing
     * @date: 2025/1/3 15:43
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        LambdaQueryWrapper<AcTicket> acqe = new LambdaQueryWrapper<>();
        acqe.eq(AcTicket::getPkId, pkId);
        AcTicket acTicket = acTicketService.getOne(acqe);
        return AjaxResult.success(acTicket);
    }

    /**
     * @description: 修改门票活动
     * @author: zhang jing
     * @date: 2025/1/3 15:59
     * @param: [acTicket]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @RepeatSubmitSimple
    @PostMapping("/update")
    @Log(module = EOperationModule.TICKET_GIVE, business = EOperationBusiness.TICKET_GIVE, method = EOperationMethod.UPDATE)
    public AjaxResult update(@Valid @RequestBody AcTicket acTicket) {
        //校验活动是否已经添加
        LambdaQueryWrapper<AcTicket> acqe = new LambdaQueryWrapper<>();
        acqe.eq(AcTicket::getActStartDate, acTicket.getActStartDate());
        acqe.eq(AcTicket::getActEndDate, acTicket.getActEndDate());
        acqe.ne(AcTicket::getPkId, acTicket.getPkId());
        List<AcTicket> list = acTicketService.list(acqe);
        if (CollectionUtil.isNotEmpty(list)) {
            return AjaxResult.error(ConfigMsgConstants.ACTIVITY_REPEAT);
        }
        acTicket.setPkModified(SecurityUtils.getUserId());
        acTicket.setModifiedTime(new Date());
        return AjaxResult.success(acTicketService.updateById(acTicket));
    }

    /**
     * @description: 删除门票活动
     * @author: zhang jing
     * @date: 2025/1/3 16:03
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.TICKET_GIVE, business = EOperationBusiness.TICKET_GIVE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        LambdaUpdateWrapper<AcTicket> at = new LambdaUpdateWrapper<>();
        at.eq(AcTicket::getPkId, pkId);
        at.set(AcTicket::getDelFlag, EDelFlag.DELETE.getValue());
        at.set(AcTicket::getPkModified, SecurityUtils.getUserId());
        at.set(AcTicket::getModifiedTime, new Date());
        return AjaxResult.success(acTicketService.update(at));
    }

}
