package com.goer.project.model.modelField.service.impl;

import java.util.Date;
import java.util.List;
import com.goer.common.utils.DateUtils;
import com.goer.common.utils.StringUtils;
import com.goer.common.utils.security.ShiroUtils;
import com.goer.project.model.modelEntity.domain.ModelEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goer.project.model.modelField.mapper.ModelFieldMapper;
import com.goer.project.model.modelField.domain.ModelField;
import com.goer.project.model.modelField.service.IModelFieldService;
import com.goer.common.utils.text.Convert;

/**
 * 数据字段Service业务层处理
 * 
 * @author goer
 * @date 2022-01-17
 */
@Service
public class ModelFieldServiceImpl implements IModelFieldService 
{
    @Autowired
    private ModelFieldMapper modelFieldMapper;

    /**
     * 查询数据字段
     * 
     * @param id 数据字段主键
     * @return 数据字段
     */
    @Override
    public ModelField selectModelFieldById(Long id)
    {
        return modelFieldMapper.selectModelFieldById(id);
    }

    /**
     * 查询数据字段列表
     * 
     * @param modelField 数据字段
     * @return 数据字段
     */
    @Override
    public List<ModelField> selectModelFieldList(ModelField modelField)
    {
        return modelFieldMapper.selectModelFieldList(modelField);
    }

    /**
     * 新增数据字段
     * 
     * @param modelField 数据字段
     * @return 结果
     */
    @Override
    public int insertModelField(ModelField modelField)
    {
        int sort = modelFieldMapper.selectMaxFieldSort(modelField);
        modelField.setFieldSort(sort);
        modelField.setCreateTime(DateUtils.getNowDate());
        return modelFieldMapper.insertModelField(modelField);
    }

    /**
     * 修改数据字段
     * 
     * @param modelField 数据字段
     * @return 结果
     */
    @Override
    public int updateModelField(ModelField modelField)
    {
        modelField.setUpdateTime(DateUtils.getNowDate());
        return modelFieldMapper.updateModelField(modelField);
    }

    /**
     * 批量删除数据字段
     * 
     * @param ids 需要删除的数据字段主键
     * @return 结果
     */
    @Override
    public int deleteModelFieldByIds(String ids)
    {
        return modelFieldMapper.deleteModelFieldByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除数据字段信息
     * 
     * @param id 数据字段主键
     * @return 结果
     */
    @Override
    public int deleteModelFieldById(Long id)
    {
        return modelFieldMapper.deleteModelFieldById(id);
    }

    /**
     * 排序
     *
     * @param modelField
     * @return
     */
    @Override
    public int sort(ModelField modelField) {
        int result = 0;

        int pageSize = 10;
        int pageNumber = 1;
        Object pz = modelField.getParams().get("pageSize");
        if (pz != null) {
            pageSize = Convert.toInt(pz, 10);
        }
        Object pn = modelField.getParams().get("pageNumber");
        if (pn != null) {
            pageNumber = Convert.toInt(pn, 1);
        }

        List<ModelField> list = modelField.getPramsByName("sortData", ModelField.class);
        if(list != null && !list.isEmpty()) {
            int idx = pageSize * (pageNumber - 1) + 1;
            for(ModelField mf : list) {
                mf.setUpdateBy(modelField.getUpdateBy());
                mf.setUpdateTime(modelField.getUpdateTime());
                mf.setFieldSort(idx ++);
            }
            result = modelFieldMapper.updateList(list);
        }
        return result;
    }

    @Override
    public String checkFieldCodeUnique(ModelField modelField) {
        Long fieldId = StringUtils.isNull(modelField.getId()) ? -1L : modelField.getId();
        ModelField info = null;
        if(StringUtils.isNotEmpty(modelField.getFieldCode())) {
            info = modelFieldMapper.checkFieldCodeUnique(modelField);
        }
        if (StringUtils.isNull(info)) {
            return "0";
        }
        if(info.getId().longValue() != fieldId.longValue()) {
            return "1";
        }
        return "0";
    }
}
