package com.casic.missiles.modular.dept.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.dept.dto.DeptUserView;
import com.casic.missiles.modular.dept.model.BusDeptUser;
import com.casic.missiles.modular.dept.service.IBusDeptUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 控制器
 *
 * @author dev
 * @Date 2020-05-07 17:59:11
 */
@Controller
@RequestMapping("/config")
public class BusDeptUserController extends BaseController {

    @Autowired
    private IBusDeptUserService busDeptUserService;

    /**
     * 给某部门配置责任人,有则修改，无则新增
     *
     * @param deptid 组织机构id
     * @param user   用户id
     * @return
     */
    @RequestMapping("/responsiblePersonConfig")
    @ResponseBody
    public Object responsiblePersonConfig(String deptid, String user) {
        QueryWrapper<BusDeptUser> query = new QueryWrapper<>();
        query.eq("DEPT_ID", deptid);
        BusDeptUser busDeptUser = busDeptUserService.getOne(query);
        if (null == busDeptUser) {
            busDeptUser = new BusDeptUser();
            busDeptUser.setDeptid(Long.valueOf(deptid));
            busDeptUser.setUser(Long.valueOf(user));
            busDeptUserService.save(busDeptUser);
        } else {
            busDeptUser.setUser(Long.valueOf(user));
            busDeptUserService.updateById(busDeptUser);
        }
        return ResponseData.success();
    }

    @RequestMapping("/deptResponsiblePersonList")
    @ResponseBody
    public Object deptResponsiblePersonList(String deptType, String deptName) {
        List<DeptUserView> list = busDeptUserService.selectDeptUserList(deptType, deptName);
        return ResponseData.success(list);
    }

}
