package com.zanxiang.game.module.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.module.manage.pojo.dto.CpDTO;
import com.zanxiang.game.module.manage.pojo.params.CpAddUpdateParam;
import com.zanxiang.game.module.manage.pojo.params.CpListParam;
import com.zanxiang.game.module.manage.pojo.vo.CpChoiceVO;
import com.zanxiang.game.module.manage.pojo.vo.CpVO;
import com.zanxiang.game.module.manage.service.ICpService;
import com.zanxiang.game.module.mybatis.entity.Cp;
import com.zanxiang.game.module.mybatis.mapper.CpMapper;
import com.zanxiang.module.util.bean.BeanUtil;
import com.zanxiang.module.util.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author : lingfeng
 * @time : 2022-06-22
 * @description : cp管理
 */
@Slf4j
@Service
public class CpServiceImpl extends ServiceImpl<CpMapper, Cp> implements ICpService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrUpdate(CpAddUpdateParam param) {
        Long id = param.getId();
        //更新
        if (id != null) {
            Cp cp = super.getById(id);
            if (cp == null) {
                throw new BaseException("参数错误, 更新的cp信息不存在");
            }
            //更新CP名字, 不可重复
            if (!Objects.equals(cp.getCpName(), param.getCpName())) {
                //判断cp名称是否已经存在
                long count = super.count(new LambdaQueryWrapper<Cp>()
                        .eq(Cp::getCpName, param.getCpName()));
                if (count > 0) {
                    throw new BaseException("cp名称已经存在");
                }
            }
            cp.setCpName(param.getCpName());
            cp.setCompanyName(param.getCompanyName());
            cp.setLinkMan(param.getLinkMan());
            cp.setMobile(param.getMobile());
            cp.setStatus(param.getStatus());
            cp.setRemark(param.getRemark());
            cp.setUpdateTime(LocalDateTime.now());
            return super.updateById(cp);
        }
        //判断cp名称是否已经存在
        long count = super.count(new LambdaQueryWrapper<Cp>()
                .eq(Cp::getCpName, param.getCpName()));
        if (count > 0) {
            throw new BaseException("cp名称已经存在");
        }
        //创建
        return super.save(Cp.builder()
                .cpName(param.getCpName())
                .companyName(param.getCompanyName())
                .linkMan(param.getLinkMan())
                .mobile(param.getMobile())
                .status(param.getStatus())
                .remark(param.getRemark())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build());
    }

    @Override
    public IPage<CpVO> list(CpListParam param) {
        return page(param.toPage(), new QueryWrapper<Cp>().lambda()
                .like(Strings.isNotBlank(param.getCpName()), Cp::getCpName, param.getCpName())
                .orderByDesc(Cp::getCreateTime)
        ).convert(this::toVo);
    }

    private CpVO toVo(Cp cp) {
        if (Objects.isNull(cp)) {
            return null;
        }
        return BeanUtil.copy(cp, CpVO.class);
    }

    @Override
    public List<CpChoiceVO> choiceList() {
        List<Cp> cpList = super.list(new LambdaQueryWrapper<Cp>().select(Cp::getId, Cp::getCpName));
        return cpList.stream().map(cp -> BeanUtil.copy(cp, CpChoiceVO.class)).collect(Collectors.toList());
    }

    @Override
    public Map<Long, CpDTO> cpMap() {
        List<Cp> cpList = super.list(new LambdaQueryWrapper<Cp>().select(Cp::getId, Cp::getCpName));
        if (CollectionUtils.isEmpty(cpList)) {
            return Collections.emptyMap();
        }
        List<CpDTO> cpDTOList = cpList.stream().map(cp -> BeanUtil.copy(cp, CpDTO.class)).collect(Collectors.toList());
        return cpDTOList.stream().collect(Collectors.toMap(CpDTO::getId, Function.identity()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        return super.removeById(id);
    }

}
