package com.ghy.web.controller.order;

import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.order.domain.OrderTemplate;
import com.ghy.order.service.OrderTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@Controller
@RequestMapping("order/template")
public class OrderTemplateController extends BaseController {

    @Resource
    private OrderTemplateService orderTemplateService;

    @PostMapping("insert")
    @ResponseBody
    public AjaxResult insert(@RequestBody OrderTemplate orderTemplate) {
        Long userId = getUserId();
        Assert.notNull(userId, "UserId is null !");
        orderTemplate.setUserId(userId);
        orderTemplate.setCreateBy(getLoginName());
        orderTemplateService.insert(orderTemplate);
        return AjaxResult.success();
    }

    @DeleteMapping("delete")
    @ResponseBody
    public int delete(Long id) {
        return orderTemplateService.delete(id);
    }

    @PutMapping("update")
    public int update(@RequestBody OrderTemplate orderTemplate) {
        return orderTemplateService.update(orderTemplate);
    }

    @ResponseBody
    @GetMapping("selectByUserId")
    public List<OrderTemplate> selectByUserId() {
        Long userId = getUserId();
        return orderTemplateService.selectByUserId(userId);
    }
}
