package com.mosty.qwzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.qwzx.TbQwSpsHdgjChangeDto;
import com.mosty.base.model.entity.qwzx.TbQwSpsCsdw;
import com.mosty.base.model.entity.qwzx.TbQwSpsCsdwhdgj;
import com.mosty.base.model.query.qwzx.TbQwSpsCsdwQuery;
import com.mosty.base.model.vo.qwzx.TbQwSpsCsdwVo;
import com.mosty.base.model.vo.qwzx.TbQwSpsCsdwhdgjVo;
import com.mosty.base.utils.DateUtils;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.qwzx.mapper.TbQwSpsCsdwMapper;
import com.mosty.qwzx.mapper.TbQwSpsCsdwhdgjMapper;
import com.mosty.qwzx.service.TbQwSpsCsdwService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author dw
 * @since 2022/9/17
 **/
@Service
@AllArgsConstructor
public class TbQwSpsCsdwServiceImpl extends ServiceImpl<TbQwSpsCsdwMapper, TbQwSpsCsdw>
        implements TbQwSpsCsdwService {

    private final TbQwSpsCsdwhdgjMapper tbQwSpsCsdwhdgjMapper;

    @Override
    public Object getList(TbQwSpsCsdwQuery dto) {
        List<String> dqwzList = null;
        if (dto.getDqwzs() != null) {
            dqwzList = Arrays.asList(dto.getDqwzs().split(","));
        }
        if (dto.getPageCurrent() == null || dto.getPageSize() == null) {
            return this.baseMapper.selectList(
                    new LambdaQueryWrapper<TbQwSpsCsdw>()
                            .in(!CollectionUtils.isEmpty(dqwzList), TbQwSpsCsdw::getDqwz, dqwzList)
                            .like(StringUtils.isNotBlank(dto.getCddwmc()), TbQwSpsCsdw::getCddwmc, dto.getCddwmc())
                            .eq(StringUtils.isNotBlank(dto.getSsxz()), TbQwSpsCsdw::getSsxz, dto.getSsxz())
            );
        } else {
            return this.baseMapper.selectPage(
                    new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                    new LambdaQueryWrapper<TbQwSpsCsdw>()
                            .in(!CollectionUtils.isEmpty(dqwzList), TbQwSpsCsdw::getDqwz, dqwzList)
                            .like(StringUtils.isNotBlank(dto.getCddwmc()), TbQwSpsCsdw::getCddwmc, dto.getCddwmc())
                            .eq(StringUtils.isNotBlank(dto.getSsxz()), TbQwSpsCsdw::getSsxz, dto.getSsxz())
            );
        }
    }

    @Override
    public TbQwSpsCsdwVo getCsdwInfo(String id) {
        TbQwSpsCsdw dw = this.baseMapper.selectById(id);
        if (dw == null) {
            throw new BusinessException("没有查询到参赛队伍信息");
        }
        TbQwSpsCsdwVo vo = new TbQwSpsCsdwVo();
        BeanUtils.copyProperties(dw, vo);
        TbQwSpsCsdwhdgj gj = this.tbQwSpsCsdwhdgjMapper.selectOne(
                new LambdaQueryWrapper<TbQwSpsCsdwhdgj>()
                        .eq(TbQwSpsCsdwhdgj::getCddwid, id)
                        .orderByDesc(TbQwSpsCsdwhdgj::getHdsj)
                        .last(" limit 1")
        );
        if (gj != null) {
            TbQwSpsCsdwhdgjVo gjVo = new TbQwSpsCsdwhdgjVo();
            BeanUtils.copyProperties(gj, gjVo);
            vo.setGjVo(gjVo);
        }
        return vo;
    }

    @Override
    @Transactional
    public void changeHdgj(TbQwSpsHdgjChangeDto dto) {
        UserInfo user = UserInfoManager.get();
        List<String> ids = dto.getIds();
        ids.forEach(id -> {
            TbQwSpsCsdw dw = this.baseMapper.selectById(id);
            if (dw != null) {
                dw.setDqwz(dto.getHdlx());
                dw.setDqwzsj(new Date());
                if (user != null) {
                    dw.setSsbmdm(String.valueOf(user.getDeptId()));
                    dw.setSsbm(user.getDeptName());
                    dw.setSsxgaj(user.getFxjDeptName());
                    dw.setSsxgajdm(String.valueOf(user.getFxjDeptId()));
                    dw.setSssgaj(user.getDszDeptName());
                    dw.setSssgajdm(String.valueOf(user.getDszDeptId()));
                }
                dw.setXtSjly("2");
                this.baseMapper.updateById(dw);
                TbQwSpsCsdwhdgj gj = new TbQwSpsCsdwhdgj();
                gj.setCddwid(id);
                gj.setCddwmc(dw.getCddwmc());
                gj.setSsxz(dw.getSsxz());
                gj.setHdlx(dto.getHdlx());
                gj.setHdrq(new Date());
                gj.setHdsj(new Date());
                if (user != null) {
                    gj.setSsbmdm(String.valueOf(user.getDeptId()));
                    gj.setSsbm(user.getDeptName());
                    gj.setSsxgaj(user.getFxjDeptName());
                    gj.setSsxgajdm(String.valueOf(user.getFxjDeptId()));
                    gj.setSssgaj(user.getDszDeptName());
                    gj.setSssgajdm(String.valueOf(user.getDszDeptId()));
                }
                gj.setXtSjly("2");
                this.tbQwSpsCsdwhdgjMapper.insert(gj);
            }
        });
    }
}
