package com.hzs.activity.ticket.controller;

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.enums.EDelFlag;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
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.Date;
import java.util.List;

/**
 * 门票 前端控制器
 */
@RestController
@RequestMapping("/manage/ticket")
public class AcTicketController extends BaseController {

    @Autowired
    private IAcTicketService iAcTicketService;

    /**
     * 门票活动列表
     **/
    @Log(module = EOperationModule.TICKET_GIVE, business = EOperationBusiness.TICKET_GIVE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(AcTicket acTicket) {
        startPage();
        LambdaQueryWrapper<AcTicket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AcTicket::getPkCountry, SecurityUtils.getPkCountry());
        if (StringUtils.isNotEmpty(acTicket.getActName())) {
            queryWrapper.like(AcTicket::getActName, acTicket.getActName());
        }
        if (StringUtils.isNotNull(acTicket.getActStartDate())) {
            queryWrapper.ge(AcTicket::getActStartDate, acTicket.getActStartDate());
        }
        if (StringUtils.isNotNull(acTicket.getActEndDate())) {
            queryWrapper.le(AcTicket::getActEndDate, acTicket.getActEndDate());
        }
        List<AcTicket> list = iAcTicketService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 新增门票活动
     **/
    @RepeatSubmitSimple
    @PostMapping("/save")
    @Log(module = EOperationModule.TICKET_GIVE, business = EOperationBusiness.TICKET_GIVE, method = EOperationMethod.INSERT)
    public AjaxResult save(@Valid @RequestBody AcTicket acTicket) {
        acTicket.setCreationTime(new Date());
        acTicket.setPkCreator(SecurityUtils.getUserId());
        acTicket.setPkCountry(SecurityUtils.getPkCountry());
        if (StringUtils.isNotEmpty(acTicket.getPkVertex())) {
            acTicket.setPkVertex("," + acTicket.getPkVertex() + ",");
        }
        return AjaxResult.success(iAcTicketService.save(acTicket));
    }

    /**
     * 查询单条新零售门票活动信息
     **/
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        LambdaQueryWrapper<AcTicket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AcTicket::getPkId, pkId);
        AcTicket acTicket = iAcTicketService.getOne(queryWrapper);
        if (null != acTicket
                && StringUtils.isNotEmpty(acTicket.getPkVertex())
                && acTicket.getPkVertex().startsWith(",")) {
            // 返回的时候，去除前后的，拼接字符
            acTicket.setPkVertex(acTicket.getPkVertex().substring(1, acTicket.getPkVertex().length() - 1));
        }
        return AjaxResult.success(acTicket);
    }

    /**
     * 修改门票活动
     **/
    @RepeatSubmitSimple
    @PostMapping("/update")
    @Log(module = EOperationModule.TICKET_GIVE, business = EOperationBusiness.TICKET_GIVE, method = EOperationMethod.UPDATE)
    public AjaxResult update(@Valid @RequestBody AcTicket acTicket) {
        acTicket.setPkModified(SecurityUtils.getUserId());
        acTicket.setModifiedTime(new Date());
        if (StringUtils.isNotEmpty(acTicket.getPkVertex())) {
            acTicket.setPkVertex("," + acTicket.getPkVertex() + ",");
        }
        return AjaxResult.success(iAcTicketService.updateById(acTicket));
    }

    /**
     * 删除门票活动
     **/
    @Log(module = EOperationModule.TICKET_GIVE, business = EOperationBusiness.TICKET_GIVE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        LambdaUpdateWrapper<AcTicket> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AcTicket::getPkId, pkId);
        updateWrapper.set(AcTicket::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(AcTicket::getPkModified, SecurityUtils.getUserId());
        updateWrapper.set(AcTicket::getModifiedTime, new Date());
        return AjaxResult.success(iAcTicketService.update(updateWrapper));
    }

}
