package com.ics.admin.service.impl.meeting;

import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ics.admin.domain.meeting.RoomContent;
import com.ics.admin.domain.meeting.Ticket;
import com.ics.admin.domain.meeting.vo.UserCustomerVo;
import com.ics.admin.mapper.IcsCustomerStaffMapper;
import com.ics.admin.mapper.meeting.RoomContentMapper;
import com.ics.admin.mapper.meeting.TicketMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ics.admin.mapper.meeting.CustomerTicketMapper;
import com.ics.admin.domain.meeting.CustomerTicket;
import com.ics.admin.service.meeting.ICustomerTicketService;

/**
 * 企业优惠卷关联Service业务层处理
 * 
 * @author chen
 * @date 2024-02-25
 */
@Service
public class CustomerTicketServiceImpl extends ServiceImpl<CustomerTicketMapper, CustomerTicket> implements ICustomerTicketService {
    @Autowired
    private CustomerTicketMapper customerTicketMapper;

    @Autowired
    private IcsCustomerStaffMapper customerStaffMapper;

    @Autowired
    private TicketMapper ticketMapper;

    @Autowired
    private RoomContentMapper roomContentMapper;

    /**
     * 查询企业优惠卷关联
     * 
     * @param id 企业优惠卷关联ID
     * @return 企业优惠卷关联
     */
    @Override
    public CustomerTicket selectCustomerTicketById(Long id) {
        return customerTicketMapper.selectById(id);
    }

    /**
     * 查询企业优惠卷关联列表
     * 
     * @param customerTicket 企业优惠卷关联
     * @return 企业优惠卷关联
     */
    @Override
    public List<CustomerTicket> selectCustomerTicketList(CustomerTicket customerTicket) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("customer_id", customerTicket.getCustomerId());
        queryWrapper.eq("ticket_id", customerTicket.getTicketId());
        queryWrapper.eq(customerTicket.getIsVerification() !=null,"is_verification", customerTicket.getIsVerification());
        return customerTicketMapper.selectList(queryWrapper);
    }

    /**
     * 新增企业优惠卷关联
     * 
     * @param customerTicket 企业优惠卷关联
     * @return 结果
     */
    @Override
    public int insertCustomerTicket(CustomerTicket customerTicket) {
        return customerTicketMapper.insert(customerTicket);
    }

    /**
     * 修改企业优惠卷关联
     * 
     * @param customerTicket 企业优惠卷关联
     * @return 结果
     */
    @Override
    public int updateCustomerTicket(CustomerTicket customerTicket) {
        return customerTicketMapper.updateById(customerTicket);
    }

    /**
     * 删除企业优惠卷关联对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCustomerTicketByIds(String ids) {
        String[] idsArray = StrUtil.split(ids,",");
        return customerTicketMapper.deleteBatchIds(CollUtil.toList(idsArray));
    }

    /**
     * 删除企业优惠卷关联信息
     * 
     * @param id 企业优惠卷关联ID
     * @return 结果
     */
    @Override
    public int deleteCustomerTicketById(Long id) {
        return customerTicketMapper.deleteCustomerTicketById(id);
    }

    @Override
    public List<CustomerTicket> getCustomerTicket(UserCustomerVo userCustomerVo) {

        RoomContent roomContent = roomContentMapper.selectRoomContentById(userCustomerVo.getMeetingId());
        if (roomContent.getIsTicket() == 1){
            return new ArrayList<CustomerTicket>();
        }
        List<CustomerTicket> customerTickets = customerTicketMapper.selectListByCustomerId(userCustomerVo);

        for (CustomerTicket customerTicket : customerTickets) {
            Ticket ticket = ticketMapper.selectById(customerTicket.getTicketId());
            customerTicket.setTicketName(ticket.getTitle());
            customerTicket.setStartTime(ticket.getStartTime());
            customerTicket.setEndDate(ticket.getEndDate());
            customerTicket.setContent(ticket.getContent());
            customerTicket.setContent(ticket.getContent());
            customerTicket.setDuration(ticket.getDuration());
            customerTicket.setType(ticket.getType());
            customerTicket.setDiscount(ticket.getDiscount());
            int i = customerTicketMapper.selectByTicketIdAndCustomer(customerTicket.getTicketId(), customerTicket.getCustomerId());
            customerTicket.setNum(i);

        }
        return customerTickets;
    }

    @Override
    public void updateCustomerTicketBYUserId(CustomerTicket customerTicket) {
        QueryWrapper<CustomerTicket> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", customerTicket.getCustomerId());
        wrapper.eq("ticket_id", customerTicket.getTicketId());
        wrapper.eq("is_verification", 0);
        wrapper.groupBy("ticket_id");
        CustomerTicket customerTicket1 = customerTicketMapper.selectOne(wrapper);
        customerTicket1.setStaffId(customerTicket.getStaffId());
        customerTicket1.setIsVerification(1);
        baseMapper.updateCustomerTicket(customerTicket1);
    }

    @Override
    public List<CustomerTicket> selectCustomerTicketByTicketId(Long id) {
        QueryWrapper<CustomerTicket> wrapper = new QueryWrapper<>();
        wrapper.eq("ticket_id", id);
        wrapper.groupBy("customer_id");
        return customerTicketMapper.selectList(wrapper);
    }

    @Override
    public List<CustomerTicket> selectListByTicketId(Long id) {
        return customerTicketMapper.selectListByTicketId(id);


    }

    @Override
    public int selectByTicketIdAndCustomer(Long id, Long customerId) {
        return  customerTicketMapper.selectByTicketIdAndCustomer(id, customerId);
    }

    @Override
    public void deleteCustomerTicketByTicketId(Long id) {

        customerTicketMapper.deleteCustomerTicketByTicketId(id);
    }

    @Override
    public List<CustomerTicket> selectCustomerByTicketId(Long id) {
        QueryWrapper<CustomerTicket> wrapper = new QueryWrapper<>();
        wrapper.eq("ticket_id", id);
        wrapper.eq("is_verification", 0);
        wrapper.groupBy("customer_id");
        return customerTicketMapper.selectList(wrapper);
    }

    @Override
    public void deleteCustomerTicketByTicketIdAndCustomer(Long id, Long aLong) {
        customerTicketMapper.deleteCustomerTicketByTicketIdAndCustomer(id, aLong);
    }
}
