package com.xhpc.activity.controller;


import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.poi.ExcelUtil;
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.XhpcStationDeviceDomain;
import com.xhpc.activity.service.WorkStationService;
import com.xhpc.activity.service.WorkUserService;
import com.xhpc.activity.vo.XhpcStationDeviceVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@RestController
@RequestMapping("/station")
public class WorkStationController extends BaseController {


    @Resource
    WorkStationService stationService;

    @Resource
    WorkUserService userService;

    @Resource
    LogUserUtils logUserUtils;

    @GetMapping("/getPage")
    public TableDataInfo getPage(HttpServletRequest request, String stationName,
                                 String terminalName,String  stationId) {

        startPage();
        LoginUser logUser = logUserUtils.getLogUser(request);

        Map<String, Object> params = new HashMap<>();
        params.put("stationName", stationName);
        params.put("terminalName", terminalName);
        params.put("tenantId", logUser.getTenantId());
        params.put("stationId", stationId);
        return getDataTable(stationService.getStationDevice(params));
    }


    @GetMapping("/getPageByParent")
    public TableDataInfo getPageByParent(HttpServletRequest request,
                                         String stationId,
                                         String deviceType,
                                         String parentId) {
        startPage();
        LoginUser logUser = logUserUtils.getLogUser(request);

        Map<String, Object> params = new HashMap<>();
        params.put("stationId", stationId);
        params.put("deviceType", deviceType);
        params.put("parentId", parentId);
        params.put("tenantId", logUser.getTenantId());

        return getDataTable(stationService.getStationDeviceByParent(params));
    }


    @GetMapping("/getStationPage")
    public TableDataInfo getStationPage(String tenantId) {

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

        return getDataTable(stationService.getStationList(params));
    }


    @GetMapping("/getStationList")
    public R getStationList(HttpServletRequest request) {
        LoginUser logUser = logUserUtils.getLogUser(request);
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", logUser.getTenantId());
        params.put("delFlag", 0);

        return R.ok(stationService.getStationList(params));
    }


    @GetMapping("/getTree")
    public TableDataInfo getTree(
            HttpServletRequest request,
            String deviceName,
            String stationName,
            String terminalName) {

        LoginUser logUser = logUserUtils.getLogUser(request);
        Map<String, Object> params = new HashMap<>();
        params.put("stationName", stationName);
        params.put("terminalName", terminalName);
        params.put("deviceName", deviceName);
        params.put("tenantId", logUser.getTenantId());

        return getDataTable(stationService.getStationDeviceTree(params));
    }


    @Log(title = "场站设备-导入设备", businessType = BusinessType.INSERT)
    @PostMapping("/importData")
    public R importDevice(MultipartFile file, boolean updateSupport) throws Exception {

        if (file == null) {
            return R.fail("没有可导入的数据文件");
        }
        ExcelUtil<XhpcStationDeviceDomain> util = new ExcelUtil<XhpcStationDeviceDomain>(XhpcStationDeviceDomain.class);
        List<XhpcStationDeviceDomain> deviceDomainList = util.importExcel(file.getInputStream());

        String message = stationService.importDevice(deviceDomainList, updateSupport);
        return R.ok(message);
    }


    @GetMapping("/detail")
    public R getDeviceDetail(@RequestParam("deviceId") Long deviceId) throws Exception {

        return R.ok(stationService.getDeviceDetail(deviceId));
    }

    @Log(title = "场站设备-新增设备", businessType = BusinessType.INSERT)
    @PostMapping("/")
    public R insertNewDevice(HttpServletRequest request, @RequestBody XhpcStationDeviceDomain domain) throws Exception {
        LoginUser logUser = logUserUtils.getLogUser(request);
        domain.setCreateBy(logUser.getUserid().toString());
        domain.setUpdateBy(logUser.getUserid().toString());
        domain.setTenantId(logUser.getTenantId());
        return R.ok(stationService.insertDevice(domain));
    }


    @Log(title = "场站设备-新增设备(简易)", businessType = BusinessType.INSERT)
    @PostMapping("/simple")
    public R insertNewSimpleDevice(HttpServletRequest request, @RequestBody XhpcStationDeviceVo vo) throws Exception {

        LoginUser logUser = logUserUtils.getLogUser(request);
        vo.setTenantId(logUser.getTenantId());
        vo.setUserId(logUser.getUserid().toString());
        return R.ok(stationService.insertSimpleDevice(vo));
    }


    @Log(title = "场站设备-维护设备信息", businessType = BusinessType.UPDATE)
    @PutMapping("/detail")
    public R updateDeviceInfo(@RequestBody XhpcStationDeviceDomain domain) {

        return R.ok(stationService.updateDevice(domain));
    }


    @Log(title = "场站设备-删除设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/detail")
    public R deleteDeviceInfo(@RequestParam("deviceId") Long deviceId) throws Exception {

        return R.ok(stationService.deleteDevice(deviceId));
    }


    @GetMapping("/user")
    public R getDeptAndUser(HttpServletRequest request) {

        LoginUser logUser = logUserUtils.getLogUser(request);

        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", logUser.getTenantId());
        return R.ok(userService.getDeptAndUserByMap(params));
    }

}
