package com.qryord.project.master.field.service.impl;

import com.qryord.common.enums.EDelete;
import com.qryord.common.utils.DateUtils;
import com.qryord.common.utils.security.ShiroUtils;
import com.qryord.common.utils.text.Convert;
import com.qryord.project.master.field.domain.MField;
import com.qryord.project.master.field.mapper.MFieldMapper;
import com.qryord.project.master.field.service.IMFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 字段Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-06
 */
@Service
public class MFieldServiceImpl implements IMFieldService 
{
    @Autowired
    private MFieldMapper mFieldMapper;

    /**
     * 查询字段
     * 
     * @param id 字段主键
     * @return 字段
     */
    @Override
    public MField selectMFieldById(Long id)
    {
        return mFieldMapper.selectMFieldById(id);
    }

    /**
     * 查询字段列表
     * 
     * @param mField 字段
     * @return 字段
     */
    @Override
    public List<MField> selectMFieldList(MField mField)
    {
        return mFieldMapper.selectMFieldList(mField);
    }

    /**
     * 新增字段
     * 
     * @param mField 字段
     * @return 结果
     */
    @Override
    public int insertMField(MField mField)
    {
        mField.setCreateTime(DateUtils.getNowDate());
        return mFieldMapper.insertMField(mField);
    }

    /**
     * 修改字段
     * 
     * @param mField 字段
     * @return 结果
     */
    @Override
    public int updateMField(MField mField)
    {
        mField.setUpdateTime(DateUtils.getNowDate());
        return mFieldMapper.updateMField(mField);
    }

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

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

    @Transactional
    @Override
    public int dragSorted(MField mField) {

        if (mField.getParams().containsKey("inputSort")) {
            inputSort(mField);
            return 1;

        } else {

            int result = 0;
            int pageSize = 10;
            int pageNumber = 1;
            Object pz = mField.getParams().get("pageSize");
            if (pz != null) {
                pageSize = Convert.toInt(pz, 10);
            }
            Object pn = mField.getParams().get("pageNumber");
            if (pn != null) {
                pageNumber = Convert.toInt(pn, 1);
            }
            List<MField> sorteds = mField.getPramsByName("sorted", MField.class);
            if (!sorteds.isEmpty()) {
                int sort = pageSize * (pageNumber - 1) + 1;
                for (MField entity : sorteds) {
                    entity.setUpdateBy(ShiroUtils.getLoginName());
                    entity.setUpdateTime(new Date());
                    entity.setSort(sort++);
                }
                result = mFieldMapper.updateList(sorteds);
            }
            return result;
        }
    }


    public void inputSort(MField mField) {
        Object obj = mField.getParams().get("inputSort");
        int inputSort = Integer.parseInt((String) obj);

        MField cond = new MField();
        cond.setDelFlag(EDelete.FALSE.getVal());
        List<MField> fields = mFieldMapper.selectMFieldList(cond);
        if (fields != null && !fields.isEmpty()) {

            int size = fields.size();
            int newIdx = inputSort;
            if (inputSort >= size) {
                newIdx = size - 1;
            } else {
                if (newIdx <= 0) {
                    newIdx = 0;
                } else {
                    newIdx = newIdx - 1;
                }
            }

            String prodTableId = mField.getId() + "";
            LinkedList<MField> newProds = new LinkedList<>(fields);

            int oldIndex = -1;
            for (int i = 0; i < size; i++) {
                MField data = newProds.get(i);
                String oldId = data.getId() + "";
                if (oldId.equals(prodTableId)) {
                    oldIndex = i;
                    break;
                }
            }

            if (oldIndex >= 0 && oldIndex != newIdx) {

                for (int i = 0; i < size; i++) {
                    MField data = newProds.get(i);
                    String recordId = data.getId() + "";
                    if (recordId.equals(prodTableId)) {
                        newProds.remove(oldIndex);
                        newProds.add(newIdx, data);
                        break;
                    }
                }

                for (int i = 0; i < size; i++) {
                    MField data = newProds.get(i);
                    data.setSort(i + 1);
                }

                // 1000个以内
                mFieldMapper.updateList(newProds);
            }
        }
    }
}
