package com.xhpc.activity.controller;


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 com.xhpc.activity.domain.XhpcWorkTypeDictDomain;
import com.xhpc.activity.service.WorkTypeService;
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("/type")
public class WorkTypeController extends BaseController {

    @Resource
    WorkTypeService typeService;

    @Resource
    LogUserUtils logUserUtils;


    @GetMapping("/getPage")
    public TableDataInfo getPage(HttpServletRequest request){

        LoginUser loginUser = logUserUtils.getLogUser(request);

        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", loginUser.getTenantId());

        return getDataTable(typeService.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(typeService.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(typeService.getTree(params));
    }


    @GetMapping("/detail")
    public R getTypeInfo(@RequestParam Integer typeId){

        return R.ok(typeService.getInfoByPk(typeId));
    }


    @PostMapping("/")
    @Log(title = "工单类型-新增工单类型", businessType = BusinessType.INSERT)
    public R insertTypeInfo(HttpServletRequest request, @RequestBody XhpcWorkTypeDictDomain domain){
        LoginUser loginUser = logUserUtils.getLogUser(request);
        domain.setCreateBy(loginUser.getUsername());
        domain.setUpdateBy(loginUser.getUsername());
        domain.setTenantId(loginUser.getTenantId());

        return R.ok(typeService.insertTypeInfo(domain));
    }


    @PutMapping("/detail")
    @Log(title = "工单类型-更新工单类型信息", businessType = BusinessType.UPDATE)
    public R updateTypeInfo(HttpServletRequest request, @RequestBody XhpcWorkTypeDictDomain domain){
        LoginUser loginUser = logUserUtils.getLogUser(request);

        domain.setTenantId(loginUser.getTenantId());
        domain.setUpdateBy(loginUser.getUsername());
        return R.ok(typeService.updateTypeInfo(domain));
    }


    @DeleteMapping("/{typeId}")
    @Log(title = "工单类型-删除工单类型", businessType = BusinessType.DELETE)
    public R deleteTypeInfo(@PathVariable("typeId") Integer typeId){

        return R.ok(typeService.deleteTypeInfo(typeId));
    }
}
