package com.huimv.receive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.Farm;
import com.huimv.receive.entity.SysAccountMultilevel;
import com.huimv.receive.mapper.FarmMapper;
import com.huimv.receive.mapper.SysAccountMultilevelMapper;
import com.huimv.receive.service.IFarmService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <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);
    }

    @Override
    public Result listPage(Integer userId, Map paramMap) {
        int pageNum = (Integer)paramMap.get("pageNum");
        int pageSize = (Integer)paramMap.get("pageSize");
        String farmName = (String)paramMap.get("farmName");
        QueryWrapper<Farm> wrapper= new QueryWrapper<>();
        wrapper.like("farm_name",farmName);
        SysAccountMultilevel sysAccountMultilevel = accountMultilevelMapper.selectById(userId);
        String farmIds = sysAccountMultilevel.getFarmIds();
        if (StringUtils.isBlank(farmIds)){
            return new Result(10001,"未绑定牧场",false);
        }
        if (userId != 1){
            List<String> list1 = Arrays.asList(farmIds.split(","));
            wrapper.in("id",list1);
        }
        IPage<Farm> page = this.page(new Page<>(pageNum, pageSize), wrapper);
        return new Result(ResultCode.SUCCESS,page);
    }
}
