package com.huimv.admin.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.admin.system.entity.Farm;
import com.huimv.admin.system.entity.SysAccountMultilevel;
import com.huimv.admin.system.mapper.FarmMapper;
import com.huimv.admin.system.mapper.SysAccountMultilevelMapper;
import com.huimv.admin.system.service.IFarmService;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-10-14
 */
@Service
public class FarmServiceImpl extends ServiceImpl<FarmMapper, Farm> implements IFarmService {

    @Autowired
    private  SysAccountMultilevelMapper accountMultilevelMapper;
    @Override
    public Result getFarm(Integer userId) {
        SysAccountMultilevel sysAccountMultilevel = accountMultilevelMapper.selectById(userId);
        String farmIds = sysAccountMultilevel.getFarmIds();
        List list ;
        if (userId == 1){
            list = this.list();
        }else {
            List<String> list1 = Arrays.asList(farmIds.split(","));
           list = (List)this.listByIds(list1);
        }
        return new Result(ResultCode.SUCCESS,list);
    }

    @Override
    public Result add(Farm farm) {
        this.save(farm);

        return new Result(10000,"添加成功",true);
    }

    @Override
    public Result getFarm2(Integer userId) {
        SysAccountMultilevel sysAccountMultilevel = accountMultilevelMapper.selectById(userId);
        String farmIds = sysAccountMultilevel.getFarmIds();
        List<Farm> list ;
        if (userId == 1){
            list = this.list();
        }else {
            List<String> list1 = Arrays.asList(farmIds.split(","));
            list = this.listByIds(list1);
        }
        String str = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q";
        String[] split = str.split(",");
        Integer i = 0;
        for (Farm farm : list) {
            farm.setFarmName(split[i]+"牧场");
            i++;
        }
        return new Result(ResultCode.SUCCESS,list);
    }
}
