package easydo.technology.controller;

import easydo.technology.model.PurchasePlan;
import easydo.technology.service.PurchasePlanService;
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("/purchasePlan")
public class PurchasePlanController {

    @Resource
    private PurchasePlanService purchasePlanService;

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

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

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

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