package com.xhpc.activity.controller;


import com.xhpc.activity.domain.XhpcWorkUserDomain;
import com.xhpc.activity.service.WorkUserService;
import com.xhpc.activity.vo.SysUserResetPasswordVo;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.StringUtils;
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/user")
public class WorkUserController extends BaseController {

    @Resource
    WorkUserService workUserService;

    @Resource
    LogUserUtils logUserUtils;

    @GetMapping("/getPage")
    public TableDataInfo getPage(HttpServletRequest request,
                                 String deptId,
                                 String userName,
                                 String loginName,
                                 String phone,
                                 String email,
                                 Integer type) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        startPage();
        if(StringUtils.isNotEmpty(deptId)){
            deptId = deptId.replaceAll("d_", "");
        }

        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());
        params.put("deptId", deptId);
        params.put("userName", userName);
        params.put("loginName", loginName);
        params.put("phone", phone);
        params.put("email", email);
        params.put("type", type);
        return getDataTable(workUserService.getPage(params));
    }


    @GetMapping("/getList")
    public R getList(HttpServletRequest request) {

        LoginUser loginUser = logUserUtils.getLogUser(request);

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


    @GetMapping("/getTree")
    public R getTree(HttpServletRequest request) {

        LoginUser loginUser = logUserUtils.getLogUser(request);

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


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

        return R.ok(workUserService.findByPk(userId));
    }


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

        LoginUser loginUser = logUserUtils.getLogUser(request);

        domain.setTenantId(loginUser.getTenantId());
        domain.setCreateBy(loginUser.getUserid().toString());
        domain.setUpdateBy(loginUser.getUserid().toString());
        return R.ok(workUserService.insertDomain(domain));
    }


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

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


    @Log(title = "工单管理-更新运维人员密码", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public R resetDomainPwd(HttpServletRequest request, @RequestBody SysUserResetPasswordVo vo) {

        LoginUser loginUser = logUserUtils.getLogUser(request);
        vo.setTenantId(loginUser.getTenantId());
        return R.ok(workUserService.resetPassword(vo));
    }


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

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


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

        return R.ok(workUserService.deleteDomain(userId));
    }

}
