package com.njcn.serviceimpl.user;

import com.njcn.mapper.user.DeptsLineMapper;
import com.njcn.pojo.user.DeptsLine;
import com.njcn.service.user.DeptsLineService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class DeptsLineServiceImpl implements DeptsLineService {
    @Autowired
    private DeptsLineMapper deptsLineMapper;

    @Autowired
    UserUtil userUtil;

    @Override
    public List<DeptsLine> getBind(String deptsIndex) {
        List<DeptsLine> deptsLines = deptsLineMapper.getBind(deptsIndex);
        return deptsLines;
    }

    @Override
    public String getBindLine(String deptsIndex, List<String> index) {
        try {
            for (int i = 0; i < index.size(); i++) {
                DeptsLine deptsLine = new DeptsLine();
                deptsLine.setDeptsIndex(deptsIndex);
                deptsLine.setLineIndex(Long.parseLong(index.get(i)));
                deptsLine.setSystype(userUtil.getSysType());
                deptsLineMapper.insert(deptsLine);
            }
            return "绑定监测点成功";
        } catch (Exception e) {
            return "绑定监测点失败";
        }
    }

    @Override
    public String changeBindLine(String deptsIndex, List<String> index) {
        try {
            for (int i = 0; i < index.size(); i++) {
                deptsLineMapper.changeBindLine(deptsIndex, Integer.parseInt(index.get(i)), userUtil.getSysType());
            }
            return "监测点换绑成功";
        } catch (Exception e) {
            return "监测点换绑失败";
        }
    }

    @Override
    public String deleteBindLine(String deptsIndex, List<String> index) {
        try {
            for (int i = 0; i < index.size(); i++) {
                DeptsLine deptsLine = new DeptsLine();
                deptsLine.setDeptsIndex(deptsIndex);
                deptsLine.setLineIndex(Long.parseLong(index.get(i)));
                deptsLine.setSystype(userUtil.getSysType());
                deptsLineMapper.delete(deptsLine);
            }
            return "解绑监测点成功";
        } catch (Exception e) {
            return "解绑监测点失败";
        }
    }

    @Override
    public int selectLine(String parentNode) {
        int flag = 0;
        try {
            flag = deptsLineMapper.selectLine(parentNode, TokenManager.getSysType());
        } catch (Exception e) {
            return 0;
        }
        return flag;
    }

    @Override
    public int removeBind(String parentNode) {
        int flag = 0;
        try {
            Example example1 = new Example(DeptsLine.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("deptsIndex", parentNode);
            criteria1.andEqualTo("systype", TokenManager.getSysType());
            flag = deptsLineMapper.deleteByExample(example1);
        } catch (Exception e) {
            return 0;
        }
        return flag;
    }
}
