package com.huimv.receive.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultCode;
import com.huimv.receive.entity.BaseLocation;
import com.huimv.receive.entity.BaseProcess;
import com.huimv.receive.entity.vo.BaseProcessByLocationVo;
import com.huimv.receive.entity.vo.BaseProcessVo;
import com.huimv.receive.entity.vo.UserAccountVo;
import com.huimv.receive.mapper.BaseProcessMapper;
import com.huimv.receive.service.IBaseLocationService;
import com.huimv.receive.service.IBaseProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 流程表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-07-21
 */
@Service
public class BaseProcessServiceImpl extends ServiceImpl<BaseProcessMapper, BaseProcess> implements IBaseProcessService {

    @Autowired
    private BaseProcessMapper baseProcessMapper;
    @Override
    public Result getOne(Map<String, String> map) {
        String id = map.get("processId");
        BaseProcess byId = this.getById(id);
        String[] allLocation = byId.getAllLocation().split(",");
        String[] allLocationId = byId.getAllLocationId().split(",");
        String[] allLocationStatus = byId.getAllLocationStatus().split(",");
        List list = new ArrayList();
        for (int i = 0; i < allLocation.length; i++) {
            BaseProcessVo baseProcessVo = new BaseProcessVo();
            baseProcessVo.setAllLocation(allLocation[i]);
            baseProcessVo.setAllLocationId(allLocationId[i]);
            baseProcessVo.setAllLocationSatus(allLocationStatus[i]);
            list.add(baseProcessVo);
        }

        return new Result(ResultCode.SUCCESS,list);
    }

    @Override
    public Result pageByLocationId(Map<String, String> map) {
        String locationId = map.get("locationId");
        String processId = map.get("processId");
        List<BaseProcessByLocationVo> baseProcessByLocationVos =  baseProcessMapper.pageByLocationId(locationId,processId);
        for (BaseProcessByLocationVo baseProcessByLocationVo : baseProcessByLocationVos) {
            String imgUrl = baseProcessByLocationVo.getImgUrl();
            if (StringUtils.isNotBlank(imgUrl)){
                List<String> list = Arrays.asList(imgUrl.split(","));
                baseProcessByLocationVo.setImgUrls(list);
            }
        }
        return new Result(ResultCode.SUCCESS,baseProcessByLocationVos);
    }
}
