package com.huimv.beeboxs.service.impl;

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.beeboxs.common.utils.Result;
import com.huimv.beeboxs.common.utils.ResultCode;
import com.huimv.beeboxs.entity.SysAccountMultilevel;
import com.huimv.beeboxs.mapper.SysAccountMultilevelMapper;
import com.huimv.beeboxs.service.ISysAccountMultilevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-10-14
 */
@Service
public class SysAccountMultilevelServiceImpl extends ServiceImpl<SysAccountMultilevelMapper, SysAccountMultilevel> implements ISysAccountMultilevelService {

    @Autowired
    private SysAccountMultilevelMapper accountMultilevelMapper;

    @Override
    public Result findUserAccount(String searchStr, int pageNum, int pageSize, Integer userId) {

        QueryWrapper<SysAccountMultilevel> wrapper = new QueryWrapper<>();
        wrapper.like("pids",userId).or().eq("id",userId);
        IPage<SysAccountMultilevel> page = this.page(new Page<>(pageNum, pageSize), wrapper);
        return new Result(ResultCode.SUCCESS,page);
    }

    @Override
    public Result add(SysAccountMultilevel sysAccountMultilevel, Integer userId) {

        int account = this.count(new QueryWrapper<SysAccountMultilevel>().eq("account", sysAccountMultilevel.getAccount()));
        if (account > 0 ){
            return new Result(10001,"该账号已经存在，请重新创建",false);

        }

        SysAccountMultilevel byId = this.getById(userId);


        sysAccountMultilevel.setPid(userId);
        sysAccountMultilevel.setPids(byId.getPids()+","+userId);

        sysAccountMultilevel.setType(byId.getType());
        accountMultilevelMapper.insert(sysAccountMultilevel);
        return  new Result( 10000,"添加成功",true);
    }

    @Override
    public Result setAccountStatus(List<Integer> ids, Integer status) {
        try {
            for (Integer id : ids) {
                SysAccountMultilevel account= new SysAccountMultilevel();
                account.setId(id);
                account.setAccountStatus(status);
                this.updateById(account);
            }
        }catch (Exception e){
            return new Result(10001,"插入失败",false);
        }
        String message = "";
        if(status == 1){
            message = "你选择的账号已经启用。";
        }else{
            message = "你选择的账号已经暂停启用。";
        }
        return new Result(10000,message,true);
    }

    @Override
    public List<Integer> getAllChildId(Integer userId) {
        List<SysAccountMultilevel> pids = this.list(new QueryWrapper<SysAccountMultilevel>().like("pids", userId + ""));
        List<Integer> list = new ArrayList<>();
        for (SysAccountMultilevel pid : pids) {
            list.add(pid.getId());
        }
        list.add(userId);
        return list;
    }

    @Override
    public Result listSubordinate(String searchStr, int pageNum, int pageSize, Integer userId) {

        QueryWrapper<SysAccountMultilevel> wrapper = new QueryWrapper<>();
        wrapper.like("pids",userId);
        if (StringUtils.isNotBlank(searchStr)){
            wrapper.like("user_name",searchStr);
        }

        IPage<SysAccountMultilevel> page = this.page(new Page<>(pageNum, pageSize), wrapper);
        return new Result(ResultCode.SUCCESS,page);
    }

    @Override
    public Result getLastFarmId(Integer userId) {
        SysAccountMultilevel sysAccountMultilevel = accountMultilevelMapper.selectById(userId);
        Integer lastFarmId = sysAccountMultilevel.getLastFarmId();
        String farmIds = sysAccountMultilevel.getFarmIds();
        if (farmIds == null){
            return new Result(10001,"该账号没有绑定牧场",true);
        }
        if (lastFarmId == null){
            String[] split = farmIds.split(",");
            String s = split[0];
            sysAccountMultilevel.setLastFarmId(Integer.valueOf(s));
            accountMultilevelMapper.updateById(sysAccountMultilevel);
            return new Result(ResultCode.SUCCESS,s);
        }
        return new Result(ResultCode.SUCCESS,lastFarmId);
    }

}
