package cn.ey.biz.module.system.service.field;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.module.system.controller.admin.field.vo.*;
import cn.ey.biz.module.system.convert.field.FieldConvert;
import cn.ey.biz.module.system.dal.dataobject.field.FieldDO;
import cn.ey.biz.module.system.dal.mapper.field.FieldMapper;
import com.alibaba.excel.util.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.module.system.enums.ErrorCodeConstants.*;

/**
 * 字段权限-字段 Service 实现类
 *
 */
@Service
@Validated
public class FieldServiceImpl implements FieldService {

    @Resource
    private FieldMapper fieldMapper;

    @Override
    @Transactional
    public Long createField(FieldCreateReqVO createReqVO) {

       FieldDO field = FieldConvert.INSTANCE.convert(createReqVO);
       if(!StringUtils.isEmpty(createReqVO.getFieldName()) && !StringUtils.isEmpty(createReqVO.getFieldFix())){
           // 校验字段名字
           checkFieldName(createReqVO.getFieldName());
           // 插入
           fieldMapper.insert(field);
       }

        // 返回
        return field.getId();
    }

    private void checkFieldName(String fieldName) {
        FieldDO fieldDO = fieldMapper.selectOne(FieldDO::getFieldName, fieldName);
        if(fieldDO != null){
            throw exception(FIELD_NAME_NOT_REPETITION);
        }
    }

    @Override
    public void updateField(FieldUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateFieldExists(updateReqVO.getId());
        // 更新
        FieldDO updateObj = FieldConvert.INSTANCE.convert(updateReqVO);
        fieldMapper.updateById(updateObj);
    }

    @Override
    public void deleteField(Long id) {
        // 校验存在
        this.validateFieldExists(id);
        // 删除
        fieldMapper.deleteById(id);
    }

    private void validateFieldExists(Long id) {
        if (fieldMapper.selectById(id) == null) {
            throw exception(FIELD_NOT_EXISTS);
        }
    }

    @Override
    public FieldDO getField(Long id) {
        return fieldMapper.selectById(id);
    }

    @Override
    public List<FieldDO> getFieldList(Collection<Long> ids) {
        return fieldMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FieldDO> getFieldPage(FieldPageReqVO pageReqVO) {
        return fieldMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FieldDO> getFieldList(FieldExportReqVO exportReqVO) {
        return fieldMapper.selectList(exportReqVO);
    }

    @Override
    public List<FieldRespVO> getFieldListWithBelongTo(FieldExportReqVO exportReqVO) {
        List<FieldRespVO> fieldRespVOS = fieldMapper.selectListWithBelongTo(exportReqVO);
        List<FieldRespVO> fieldRespVOS2 = getFieldLists(fieldRespVOS);
        return fieldRespVOS2;
    }

    @Override
    public List<FieldRespVO> getFieldListBelongTo(String configCode) {
        List<FieldRespVO> fieldRespVOS = fieldMapper.getFieldListBelongTo(configCode);
        List<FieldRespVO> fieldRespVOS2 = getFieldLists(fieldRespVOS);
        return fieldRespVOS2;
    }

    private List<FieldRespVO> getFieldLists(List<FieldRespVO> list) {
        //  获取对应下标
        List<FieldRespVO> collect = list.stream().map(fieldRespVO -> {
            List<Integer> lis = new ArrayList<>();
            if(fieldRespVO.getBelongs() != null){
                for (FieldItemRespVO belong : fieldRespVO.getBelongs()) {
                    lis.add(Integer.valueOf(belong.getBelongTo()));
                }
            }
            fieldRespVO.setBelongTos(lis);
            fieldRespVO.setBelongs(null);
            return fieldRespVO;
        }).collect(Collectors.toList());
        // 根据下标 实现 [1,0,1,0,0,0,0,0] 格式
        for (FieldRespVO fieldRespVO : collect) {
            Integer[] arr = new Integer[]{0,0,0,0,0,0,0,0};
            if(fieldRespVO.getBelongTos()!=null){
                for (Integer integer : fieldRespVO.getBelongTos()) {
                    arr[integer] = 1;
                }
            }
            fieldRespVO.setBelongTos(Arrays.asList(arr));
        }
        return collect;
    }

}
