package cn.pluss.platform.controller.device;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.device.DeviceService;
import cn.pluss.platform.entity.Device;
import cn.pluss.platform.util.OssOperatUtil;
import cn.pluss.platform.util.PageUtils;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.Map;


@Controller
@RequestMapping("device")
public class DeviceController extends BaseNoModelController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 引导页
     *
     * @return
     */
    @GetMapping("/index")
    public String index() {
        return "device/deviceList";
    }

    @GetMapping("/page")
    @ResponseBody
    public Result<PageInfo<Device>> page(Page<Device> page, Device device) {
        Page<Device> pageDate = deviceService.pageDate(page, device);
        return ResultGenerator.genSuccessResult(PageUtils.page(pageDate));
    }

    //数据回显
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
    @ResponseBody
    public ModelAndView detail(@PathVariable(value = "id", required = false) Integer id) {
        ModelAndView view = new ModelAndView("device/deviceBaseInfo");
        if (id != null) {
            Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
            view.addObject("uploadParam", uploadParam);
            Device deviceServiceById = deviceService.getDeviceId(id);
            System.out.println("deviceServiceById=" + deviceServiceById);
            view.addObject("entity", deviceServiceById);
        } else {
            Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
            view.addObject("uploadParam", uploadParam);
        }
        return view;
    }

    //设备详情
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/particulars/{id}")
    @ResponseBody
    public ModelAndView particulars(@PathVariable(value = "id", required = false) Integer id) {
        ModelAndView view = new ModelAndView("device/particulars");
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        view.addObject("uploadParam", uploadParam);
        Device deviceServiceById = deviceService.getDeviceId(id);
        System.out.println("deviceServiceById=" + deviceServiceById);
        view.addObject("entity", deviceServiceById);
        return view;
    }

    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody Device device) {
        if (device.getId() == null) {
            device.setCreateDt(new Date());
            deviceService.save(device);
        } else {
            deviceService.updateById(device);
        }
        return ResultGenerator.genSuccessResult("保存成功");
    }

    @PostMapping("/delete")
    public Result<Device> delete(@RequestBody Device device) {
        deviceService.removeById(device.getId());
        if (StringUtil.isNotEmpty(device.getDeviceLogo())) {
            OssOperatUtil.deleteFile(device.getDeviceLogo());
        }
        return ResultGenerator.genSuccessResult("删除成功", device);
    }

}


