package easydo.technology.controller;

import easydo.technology.model.ProductPlan;
import easydo.technology.service.ProductPlanService;
import easydo.technology.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/productPlan")
public class ProductPlanController {

    @Resource
    private ProductPlanService productPlanService;

    @RequestMapping(value = "/getPage")
    public Object getPage(@RequestBody Map<String, Object> map) throws Exception {
        Map<String, Object> result = productPlanService.getPage(map);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/save")
    public Object save(@RequestBody ProductPlan model) throws Exception {
        // 校验用户登录状态
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            return new ResponseEntity<>("用户请先登录", HttpStatus.UNAUTHORIZED);
        }
        
        model.setCreateId(userId);
        return new ResponseEntity<>(productPlanService.save(model), HttpStatus.OK);
    }

    @RequestMapping(value = "/update")
    public Object update(@RequestBody ProductPlan model) throws Exception {
        // 校验用户登录状态
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            return new ResponseEntity<>("用户请先登录", HttpStatus.UNAUTHORIZED);
        }
        
        model.setUpdateId(userId);
        return new ResponseEntity<>(productPlanService.update(model), HttpStatus.OK);
    }

    @RequestMapping(value = "/remove")
    public Object remove(@RequestBody ProductPlan model) throws Exception {
        return new ResponseEntity<>(productPlanService.remove(model), HttpStatus.OK);
    }
}

