package com.sifang.modules.sunshine.SystemSetting.controller;

import com.jfinal.plugin.activerecord.Db;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmDepartment;
import com.sifang.modules.sunshine.SystemSetting.form.DepartmentVo;
import com.sifang.modules.sunshine.SystemSetting.service.DepartmentSettingService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "${adminPath}/departmentSetting")
public class DepartmentSettingController extends BaseController {

    @Autowired
    private DepartmentSettingService departmentSettingService;
    private Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value = "index")
    public String planList(Model model) {

        return "modules/yangguang/systemSetting/department/departmentList";
    }
    @RequestMapping(value = "addDep")
    public String addDep(Model model,HttpServletRequest request) {
        String id = request.getParameter("id");

        if(StringUtil.isNotBlank(id)){
            CrmDepartment dep = departmentSettingService.getById(id);
            model.addAttribute("dep",dep);

        }

        return "modules/yangguang/systemSetting/department/addDepartment";
    }



    /**
     * 系统设置  获取部门列表数据
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getDepartList")
    public Object getDepartList(HttpServletRequest request) {
        String id = request.getParameter("depId");
        List<DepartmentVo> listDep = departmentSettingService.getDepartList(id);


        return listDep;
    }

    /**
     * 保存部门
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveDepartment")
    public Object saveDepartment(HttpServletRequest request) {
        Result result = new Result();
        try {
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String code = request.getParameter("code");
            String permission = request.getParameter("permission");
            CrmDepartment department = new CrmDepartment();
            department.setId(id);
            department.setCode(code);
            department.setName(name);
             department.setParentId(permission);
           // department.setFrontDepartment(frontDepartment);
            result = departmentSettingService.saveDepartment(department);
        } catch (IllegalArgumentException i) {
            i.printStackTrace();
            result.setCode("201");
            result.setMsg(i.getMessage());
            logger.error("IllegalArgumentException =---------------  {}",i);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode("205");
            result.setMsg("保存失败！");
            logger.error("Exception =---------------  {}",e);
        }
        return result;
    }

    /**
     * 删除部门
     *
     * @param id 部门id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteDepartment")
    public Result deleteDepartment(final String id) {
        Result result = new Result();
        try {
            result = departmentSettingService.deleteDepartment(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode("205");

        }
        return result;
    }

    /**
     * 新增部门时 获的所有一级部门
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAllTopDept")
    public Object getAllTopDept() {
        return departmentSettingService.getAllTopDept();
    }

    /**
     * 编辑部门时 获得部门信息
     */
    @ResponseBody
    @RequestMapping(value = "getDeptDetail")
    public Object getDeptDetail(String id) {
        return Db.find(" SELECT d.id,d.name,d.code,d.`parent_id`,d.front_department as frontDepartment   FROM `crm_department` d WHERE d.`status`='1'  AND d.id=? ", id);
    }


    @ResponseBody
    @RequestMapping(value = "getDepartmentAll")
    public Object getDepartmentAll(HttpServletRequest request) {
        String id = request.getParameter("id");
        return departmentSettingService.getDepartmentAll(id);
    }

}
