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

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

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

    @Autowired
    private SysAccountMultilevelMapper accountMultilevelMapper;


    @Autowired
    private SysOrgMapper sysOrgMapper;


    @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 = (int)this.count(new QueryWrapper<SysAccountMultilevel>().eq("account", sysAccountMultilevel.getAccount()));
//        SysOrg sysOrg = sysOrgMapper.selectById(sysAccountMultilevel.getOrgId());

        if (account > 0 ){
            return new Result(10001,"该账号已经存在，请重新创建",false);


        }
//        sysAccountMultilevel.setPids(sysOrg.getPids());

        SysAccountMultilevel byId = this.getById(userId);
        sysAccountMultilevel.setType(byId.getType()+1);


//        sysAccountMultilevel.setOrgId(userId);
        sysAccountMultilevel.setPids(userId+","+byId.getPids());
        sysAccountMultilevel.setPid(+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(String orgId) {
        List<SysAccountMultilevel> pids = this.list(new QueryWrapper<SysAccountMultilevel>().like("pids", orgId ));
        List<Integer> list = new ArrayList<>();
        for (SysAccountMultilevel pid : pids) {
            list.add(pid.getId());
        }
        return list;
    }

//    @Override
  /*  public Result listSubordinate(String searchStr, int pageNum, int pageSize, String orgId) {

        QueryWrapper<SysAccountMultilevel> wrapper = new QueryWrapper<>();

        if ( !"0".equals(orgId)){
            List<SysOrg> sysOrgs = sysOrgMapper.selectList(new QueryWrapper<SysOrg>().like("pids", orgId));
            List list = new ArrayList();
            for (SysOrg sysOrg : sysOrgs) {
                list.add(sysOrg.getId());
            }
            if (ObjectUtil.isNotEmpty(list)){
                wrapper.in("org_id",list);
            }else {
                wrapper.eq("org_id",null);
            }

        }
        wrapper.ne("id",1);
        if (StringUtils.isNotBlank(searchStr)){
            wrapper.like("user_name",searchStr).or().like("account",searchStr);
        }

        IPage<SysAccountMultilevel> page = this.page(new Page<>(pageNum, pageSize), wrapper);
        return new Result(ResultCode.SUCCESS,page);
    }*/
    @Override
    public Result listSubordinate(String searchStr, int pageNum, int pageSize, Integer orgId) {

        QueryWrapper<SysAccountMultilevel> wrapper = new QueryWrapper<>();

//        wrapper.ne("id",orgId);
        wrapper.like("pids",orgId);
        if (StringUtils.isNotBlank(searchStr)){
            wrapper.like("user_name",searchStr).or().like("account",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);
    }
    @Autowired
    private BaseFarmMapper baseFarmMapper;

    @Override
    public Result listById(Integer userId) {
        if (1 ==userId){
            return new Result(ResultCode.SUCCESS,baseFarmMapper.selectList(null));
        }
        SysAccountMultilevel sysAccountMultilevel = accountMultilevelMapper.selectById(userId);
        String[] farmIds = sysAccountMultilevel.getFarmIds().split(",");
        return new Result(ResultCode.SUCCESS,baseFarmMapper.selectList(new QueryWrapper<BaseFarm>().in("farm_code",farmIds)));
    }

    @Override
    public Result boundFarm(Map<String, String> paramsMap) {
        String farmCodes = paramsMap.get("farmCodes");
        String userId = paramsMap.get("userId");
        SysAccountMultilevel sysAccountMultilevel = accountMultilevelMapper.selectById(userId);
        sysAccountMultilevel.setFarmIds(farmCodes);
        accountMultilevelMapper.updateById(sysAccountMultilevel);
        return new Result(10000,"绑定成功",true);
    }

    @Override
    public Result updateUserPassword(Map<String,String> map,Integer userId) {
        String oldPassWord = map.get("oldPassWord");
        String newPassWord = map.get("newPassWord");
        SysAccountMultilevel accountMultilevel = accountMultilevelMapper.selectById(userId);
        if (!accountMultilevel.getPassword().equals(oldPassWord)){
            return new Result(10001,"密码错误",false);
        }
        accountMultilevel.setPassword(newPassWord);
        accountMultilevelMapper.updateById(accountMultilevel);



        return new Result(ResultCode.SUCCESS,"修改成功");

    }


}
