package com.th.demo.service.impl.system;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.mapping.system.SysDepartmentMapper;
import com.th.demo.model.system.SysDepartment;
import com.th.demo.model.system.SysDeptTree;
import com.th.demo.model.system.TreeNode;
import com.th.demo.model.system.Type;
import com.th.demo.service.system.SysDepartmentService;
import com.th.demo.tools.JSONTools;
import com.th.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by dell on 2017-07-07.
 */

@Service("SysDepartmentService")
public class SysDepartmentServiceImpl implements SysDepartmentService {

    @Autowired
    public SysDepartmentMapper sysDepartmentMapper;

    int num = 0;
    String result=Type.FAIL;
    Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd hh:mm:ss").create();//gson json的三方框架 数据传递格式


    @Override
    public int modify(SysDepartment dept) {

        num = 0;
        num = sysDepartmentMapper.updateByPrimaryKey(dept);
        return num;
    }

    @Override
    public String query(String deptName) {
        List<SysDepartment> list = sysDepartmentMapper.selectByParam(deptName);
        List<TreeNode> tree = Tools.getDeptTree(list,"0");
        return JSON.toJSONString(tree);
    }

    @Override
    public String queryDeptTree() {
        List<SysDepartment> list = sysDepartmentMapper.selectByParam("");
        List<SysDeptTree> tree = getDeptTree(list,"0");
        return JSONTools.toString(tree);
    }

    @Override
    public List<SysDepartment> queryByFid(String fid) {
        List<SysDepartment> list = sysDepartmentMapper.selectByFid(fid);
        return list;
    }


    @Override
    public   SysDepartment queryById(String id) {
        SysDepartment  dept = sysDepartmentMapper.selectByPrimaryKey(id);
        return dept;
    }

    @Override
    public String save(String id, String fid,String deptName, String deptDesc, String userId) {
        SysDepartment sysDepartment;
        try {
            if ( id.equals("")||id.equals(null)) {
                sysDepartment = new SysDepartment();
                sysDepartment.setfId(fid);
                sysDepartment.setDeptName(deptName);
                sysDepartment.setDeptDesc(deptDesc);
                sysDepartment.setAddId(userId);
                sysDepartment.setAddTime(new Date());
                sysDepartment.setCancelFlag("0");
                num = sysDepartmentMapper.insert(sysDepartment);
                result = Tools.moreThanZero(num);
            } else {
                sysDepartment = sysDepartmentMapper.selectByPrimaryKey(id);
                sysDepartment.setfId(fid);
                sysDepartment.setDeptName(deptName);
                sysDepartment.setDeptDesc(deptDesc);
                sysDepartment.setModifyId(userId);
                sysDepartment.setModifyTime(new Date());
                num = sysDepartmentMapper.updateByPrimaryKey(sysDepartment);
                result = Tools.moreThanZero(num);
            }
        return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return result;
        }
    }

    @Override
    public String remove(String id,String userId) {
        SysDepartment sysDepartment = new SysDepartment();
        sysDepartment.setId(id);
        sysDepartment.setCancelId(userId);
        sysDepartment.setCancelTime(new Date());
        sysDepartment.setCancelFlag("9");
        num = sysDepartmentMapper.deleteByUpdate9(sysDepartment);
        result = Tools.moreThanZero(num);
        return result;
    }



    public static List<SysDeptTree> getDeptTree(List<SysDepartment> list, String pid) {
        List<SysDeptTree> tree = new ArrayList<SysDeptTree>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getfId().equals(pid)) {
                SysDeptTree sysDeptTree = new SysDeptTree();
                sysDeptTree.setSysDepartment(list.get(i));
                List<SysDeptTree> children = getDeptTree(list, list.get(i)
                        .getId() + "");
                sysDeptTree.setChildren(children);
                tree.add(sysDeptTree);
            }
        }

        return tree;
    }
}
