package com.liquidnet.service.sweet.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.commons.lang.util.DateUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.sweet.dto.SweetManualArtistListDto;
import com.liquidnet.service.sweet.dto.SweetManualArtistListMDSKDto;
import com.liquidnet.service.sweet.entity.SweetManualArtists;
import com.liquidnet.service.sweet.entity.SweetManualArtistsMdsk;
import com.liquidnet.service.sweet.mapper.SweetManualArtistsMapper;
import com.liquidnet.service.sweet.mapper.SweetManualArtistsMdskMapper;
import com.liquidnet.service.sweet.service.ISweetManualArtistsMdskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.service.sweet.utils.RedisDataUtils;
import com.liquidnet.service.sweet.utils.RedisMDSKDataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 电子宣传手册艺人表 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2021-08-09
 */
@Service
public class SweetManualArtistsMdskServiceImpl extends ServiceImpl<SweetManualArtistsMdskMapper, SweetManualArtistsMdsk> implements ISweetManualArtistsMdskService {
    @Autowired
    private SweetManualArtistsMdskMapper sweetManualArtistsMdskMapper;
    @Autowired
    private RedisMDSKDataUtils redisDataUtils;

    @Override
    public ResponseDto<PageInfo<SweetManualArtistListMDSKDto>> getList(Integer page, Integer size, String manualId) {
        PageInfo<SweetManualArtistListMDSKDto> pageInfoTmp;
        try {
            PageHelper.startPage(page, size);
            HashMap<String, Object> map = CollectionUtil.mapStringObject();
            map.put("manualId", manualId);
            List<SweetManualArtistListMDSKDto> voList = sweetManualArtistsMdskMapper.getManualList(map);
            pageInfoTmp = new PageInfo(voList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDto.failure();
        }
        return ResponseDto.success(pageInfoTmp);
    }

    @Override
    public ResponseDto<Boolean> add(String manualId, String artistId, String stageId, String performanceStart, String performanceEnd, String signatureStart, String signatureEnd) {
        try {
            SweetManualArtistsMdsk sweetManualNotify = SweetManualArtistsMdsk.getNew();
            sweetManualNotify.setManualRelationId(IDGenerator.nextSnowId());
            sweetManualNotify.setManualId(manualId);
            sweetManualNotify.setStageId(stageId);
            sweetManualNotify.setArtistsId(artistId);
            sweetManualNotify.setPerformanceEnd(performanceEnd != null ? DateUtil.Formatter.yyyyMMddHHmmss.parse(performanceEnd) : null);
            sweetManualNotify.setPerformanceStart(performanceStart != null ? DateUtil.Formatter.yyyyMMddHHmmss.parse(performanceStart) : null);
            sweetManualNotify.setSignatureStart(signatureStart != null ? DateUtil.Formatter.yyyyMMddHHmmss.parse(signatureStart) : null);
            sweetManualNotify.setSignatureEnd(signatureEnd != null ? DateUtil.Formatter.yyyyMMddHHmmss.parse(signatureEnd) : null);
            sweetManualNotify.setCreatedAt(LocalDateTime.now());
            sweetManualArtistsMdskMapper.insert(sweetManualNotify);

            redisDataUtils.deleteTimeListRedisData(manualId);
            return ResponseDto.success();
        } catch (Exception e) {
            return ResponseDto.failure();
        }
    }

    @Override
    public ResponseDto<SweetManualArtistListMDSKDto> details(String manualRelationId) {
        try {
            HashMap<String, Object> map = CollectionUtil.mapStringObject();
            map.put("manualRelationId", manualRelationId);
            SweetManualArtistListMDSKDto dto = sweetManualArtistsMdskMapper.getManualDetails(map);
            return ResponseDto.success(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDto.failure();
        }
    }

    @Override
    public ResponseDto<Boolean> change(String manualRelationId, String manualId, String artistId, String stageId, String performanceStart, String performanceEnd, String signatureStart, String signatureEnd) {
        try {
            SweetManualArtistsMdsk sweetManualNotify = SweetManualArtistsMdsk.getNew();
            sweetManualNotify.setStageId(stageId);
            sweetManualNotify.setArtistsId(artistId);
            sweetManualNotify.setPerformanceEnd(DateUtil.Formatter.yyyyMMddHHmmss.parse(performanceEnd));
            sweetManualNotify.setPerformanceStart(DateUtil.Formatter.yyyyMMddHHmmss.parse(performanceStart));
            sweetManualNotify.setSignatureStart(DateUtil.Formatter.yyyyMMddHHmmss.parse(signatureStart));
            sweetManualNotify.setSignatureEnd(DateUtil.Formatter.yyyyMMddHHmmss.parse(signatureEnd));
            sweetManualNotify.setUpdatedAt(LocalDateTime.now());
            sweetManualArtistsMdskMapper.update(sweetManualNotify, Wrappers.lambdaUpdate(SweetManualArtistsMdsk.class).eq(SweetManualArtistsMdsk::getManualRelationId, manualRelationId));

            redisDataUtils.deleteTimeListRedisData(manualId);
            return ResponseDto.success();
        } catch (Exception e) {
            return ResponseDto.failure();
        }
    }

    @Override
    public ResponseDto<Boolean> delete(String manualRelationId, String manualId) {
        try {
            SweetManualArtistsMdsk sweetManualNotify = SweetManualArtistsMdsk.getNew();
            sweetManualNotify.setManualRelationId(IDGenerator.nextSnowId());
            sweetManualNotify.setStatus(0);
            sweetManualNotify.setUpdatedAt(LocalDateTime.now());
            sweetManualArtistsMdskMapper.update(sweetManualNotify, Wrappers.lambdaUpdate(SweetManualArtistsMdsk.class).eq(SweetManualArtistsMdsk::getManualRelationId, manualRelationId));

            redisDataUtils.deleteTimeListRedisData(manualId);

            return ResponseDto.success();
        } catch (Exception e) {
            return ResponseDto.failure();
        }
    }
}
