package com.liquidnet.client.admin.zhengzai.smile.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.client.admin.common.utils.StringUtils;
import com.liquidnet.client.admin.zhengzai.smile.service.ISmileAgentService;
import com.liquidnet.client.admin.zhengzai.smile.utils.SmileRedisUtils;
import com.liquidnet.service.goblin.constant.SmileRedisConst;
import com.liquidnet.service.goblin.dto.vo.SmileAgentVo;
import com.liquidnet.service.smile.entity.SmileAgent;
import com.liquidnet.service.smile.mapper.SmileAgentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2022-03-16
 */
@Service
@Slf4j
public class SmileAgentServiceImpl extends ServiceImpl<SmileAgentMapper, SmileAgent> implements ISmileAgentService {

    @Autowired
    SmileAgentMapper smileAgentMapper;

    @Autowired
    SmileRedisUtils smileRedisUtils;

    @Override
    public SmileAgent listAgentByShowIdAndTicketId(String performanceId, String ticketId) {
        if (StringUtils.isNotEmpty(performanceId) && StringUtils.isNotEmpty(ticketId)) {
            LambdaQueryWrapper<SmileAgent> queryWrapper  = Wrappers.lambdaQuery(SmileAgent.class);
            queryWrapper.eq(SmileAgent::getPerformanceId,performanceId);
            queryWrapper.eq(SmileAgent::getTicketId,ticketId);
            smileAgentMapper.selectOne(queryWrapper);
            return null;
        }
        return null;
    }

    @Override
    public boolean create(List<SmileAgent> smileAgents) {
        boolean result = true;
        if (StringUtils.isNotEmpty(smileAgents)) {
            for (SmileAgent agent : smileAgents) {
                result = !StringUtils.isEmpty(agent.getPerformanceId()) && !StringUtils.isEmpty(agent.getTicketId());
            }
            if (result) {
                try {
                    smileAgents.forEach(smileAgent -> {
                        //是否是修改票提
                        LambdaQueryWrapper<SmileAgent> queryWrapper = Wrappers.lambdaQuery(SmileAgent.class);
                        queryWrapper.eq(SmileAgent::getPerformanceId, smileAgent.getPerformanceId());
                        queryWrapper.eq(SmileAgent::getTicketId, smileAgent.getTicketId());
                        if (smileAgentMapper.selectOne(queryWrapper) != null) {
                            //修改
                            smileAgentMapper.update(smileAgent, queryWrapper);
                            SmileAgentVo smileAgentVo = new SmileAgentVo();
                            BeanUtils.copyProperties(smileAgent, smileAgentVo);
                            smileRedisUtils.set(SmileRedisConst.SMILE_AGENT.concat(smileAgent.getPerformanceId()).concat(":").concat(smileAgent.getTicketId()), smileAgentVo);
                            log.debug("create update Agent id：{},PerformanceId:{},TicketId:{}",
                                    smileAgent.getId(),
                                    smileAgent.getPerformanceId(),
                                    smileAgent.getTicketId());
                        } else {
                            //新增
                            smileAgentMapper.insert(smileAgent);
                            SmileAgentVo smileAgentVo = new SmileAgentVo();
                            BeanUtils.copyProperties(smileAgent, smileAgentVo);
                            smileRedisUtils.set(SmileRedisConst.SMILE_AGENT.concat(smileAgent.getPerformanceId()).concat(":").concat(smileAgent.getTicketId()), smileAgentVo);
                            log.debug("create create Agent id：{},PerformanceId:{},TicketId:{}",
                                    smileAgent.getId(),
                                    smileAgent.getPerformanceId(),
                                    smileAgent.getTicketId());
                        }
                    });
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return false;
    }

    @Override
    public String getAgent(SmileAgent smileAgent) {
        return null;
    }
}
