package com.huimv.manage.sys.service.impl;

import com.huimv.manage.sys.entity.Farm;
import com.huimv.manage.sys.entity.SysAccountMultilevel;
import com.huimv.manage.sys.mapper.FarmMapper;
import com.huimv.manage.sys.mapper.SysAccountMultilevelMapper;
import com.huimv.manage.sys.service.IFarmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.manage.common.utils.Result;
import com.huimv.manage.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);
    }
}
