package com.qmth.paper.library.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.entity.BasicField;
import com.qmth.paper.library.common.entity.SysUser;
import com.qmth.paper.library.common.enums.BasicFieldEnum;
import com.qmth.paper.library.common.enums.ExceptionResultEnum;
import com.qmth.paper.library.common.mapper.BasicFieldMapper;
import com.qmth.paper.library.common.service.BasicFieldService;
import com.qmth.paper.library.common.util.ServletUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * <p>
 * 字段管理表 服务实现类
 * </p>
 *
 * @author xf
 * @since 2024-07-30
 */
@Service
public class BasicFieldServiceImpl extends ServiceImpl<BasicFieldMapper, BasicField> implements BasicFieldService {

    @Override
    public List<BasicField> listBySchoolId(Long schoolId) {
        QueryWrapper<BasicField> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasicField::getSchoolId, schoolId)
                .orderByAsc(BasicField::getId);
        return this.list(queryWrapper);
    }

    @Override
    public List<BasicField> listData() {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        List<BasicField> basicFieldList = this.listBySchoolId(schoolId);
        if (CollectionUtils.isNotEmpty(basicFieldList)) {
            return basicFieldList;
        } else {
            List<BasicField> basicFields = BasicFieldEnum.list(schoolId);
            this.saveBatch(basicFields);
            return basicFields;
        }
    }

    @Override
    public boolean savaData(List<BasicField> list) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        // 校验code是否重复
        StringJoiner stringJoiner = new StringJoiner(";");
        Map<String, List<BasicField>> codeMap = list.stream().collect(Collectors.groupingBy(BasicField::getCode));
        for (Map.Entry<String, List<BasicField>> entry : codeMap.entrySet()) {
            if (entry.getValue().size() > 1) {
                stringJoiner.add(String.format("字段代码[%s]重复", entry.getKey()));
            }
        }
        // 校验name是否重复
        Map<String, List<BasicField>> nameMap = list.stream().collect(Collectors.groupingBy(BasicField::getName));
        for (Map.Entry<String, List<BasicField>> entry : nameMap.entrySet()) {
            if (entry.getValue().size() > 1) {
                stringJoiner.add(String.format("字段名称[%s]重复", entry.getKey()));
            }
        }
        if (StringUtils.isNotBlank(stringJoiner.toString())) {
            throw ExceptionResultEnum.ERROR.exception(stringJoiner.toString());
        }
        // 保存
        if (this.removeBySchoolId(schoolId)) {
            for (BasicField basicField : list) {
                basicField.setId(SystemConstant.getDbUuid());
                basicField.setSchoolId(schoolId);
                basicField.setCreateId(sysUser.getId());
                basicField.setCreateTime(System.currentTimeMillis());
            }
            return this.saveBatch(list);
        }
        return true;
    }

    @Override
    public boolean removeBySchoolId(Long schoolId) {
        QueryWrapper<BasicField> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasicField::getSchoolId, schoolId);
        return this.remove(queryWrapper);
    }
}
