package easydo.technology.controller;

import easydo.technology.model.OutsourcingPlan;
import easydo.technology.service.OutsourcingPlanService;
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("/outsourcingPlan")
public class OutsourcingPlanController {

    @Resource
    private OutsourcingPlanService outsourcingPlanService;

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

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

        model.setCreateId(userId);
        return new ResponseEntity<>(outsourcingPlanService.save(model), HttpStatus.OK);
    }

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

        model.setUpdateId(userId);
        return new ResponseEntity<>(outsourcingPlanService.update(model), HttpStatus.OK);
    }

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