package com.xhpc.activity.controller;


import com.xhpc.activity.domain.XhpcWorkDeptDomain;
import com.xhpc.activity.service.WorkDeptService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.common.util.LogUserUtils;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/work/dept")
public class WorkDeptController extends BaseController {

    @Resource
    WorkDeptService workDeptService;

    @Resource
    LogUserUtils logUserUtils;


    @GetMapping("/getPage")
    public TableDataInfo getPage(HttpServletRequest request,
                                 @RequestParam(required = false) Integer type) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        params.put("type", type);
        return getDataTable(workDeptService.getPage(params));
    }


    @GetMapping("/getList")
    public R getList(HttpServletRequest request,
                     @RequestParam(required = false) Integer type) {

        LoginUser loginUser = logUserUtils.getLogUser(request);

        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        params.put("type", type);
        return R.ok(workDeptService.getList(params));
    }


    @GetMapping("/getTree")
    public R getTree(HttpServletRequest request,
                     @RequestParam(required = false) Integer type) {

        LoginUser loginUser = logUserUtils.getLogUser(request);

        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        params.put("type", type);
        return R.ok(workDeptService.getTree(params));
    }


    @GetMapping("/detail")
    public R getDetail(@RequestParam("deptId") Long deptId) {

        return R.ok(workDeptService.getDomainByPk(deptId));
    }


    @Log(title = "工单管理-新增运维部门", businessType = BusinessType.INSERT)
    @PostMapping("/")
    public R insertDomain(HttpServletRequest request, @RequestBody XhpcWorkDeptDomain domain) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setCreateBy(loginUser.getUserid().toString());
        domain.setUpdateBy(loginUser.getUserid().toString());
        domain.setTenantId(loginUser.getTenantId());
        return R.ok(workDeptService.insertDomain(domain));
    }


    @Log(title = "工单管理-更新运维部门", businessType = BusinessType.UPDATE)
    @PutMapping("/detail")
    public R updateDomain(HttpServletRequest request, @RequestBody XhpcWorkDeptDomain domain) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateBy(loginUser.getUserid().toString());
        return R.ok(workDeptService.updateDomain(domain));
    }


    @Log(title = "工单管理-更新运维部门状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public R updateDomainStatus(HttpServletRequest request, @RequestBody XhpcWorkDeptDomain domain) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setUpdateBy(loginUser.getUserid().toString());
        return R.ok(workDeptService.updateDomainStatus(domain));
    }


    @Log(title = "工单管理-删除运维部门", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public R deleteDomainByPk(@PathVariable("deptId") Long deptId) {

        return R.ok(workDeptService.deleteDomain(deptId));
    }


}
