package com.zbkj.admin.controller;

import com.zbkj.common.model.deliveryset.DeliverySetTime;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.DeliveryTimeRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.service.service.DeliverySetTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("api/admin/deliveryTime")
@Api(tags = "送达时间设置")
public class DeliverySetTimeController {

    @Autowired DeliverySetTimeService deliverySetTimeService;

    @PreAuthorize("hasAuthority('admin:deliveryTime:list')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParam(name="keywords", value="搜索关键字")
    public CommonResult<CommonPage<DeliverySetTime>> getList(@Validated DeliveryTimeRequest request, @Validated PageParamRequest pageParamRequest) {
        List<DeliverySetTime> list = deliverySetTimeService.getList(request, pageParamRequest);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @PreAuthorize("hasAuthority('admin:deliveryTime:save')")
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated DeliveryTimeRequest request) {
        DeliverySetTime entity = new DeliverySetTime();
        BeanUtils.copyProperties(request, entity);
        if (deliverySetTimeService.save(entity)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('admin:deliveryTime:delete')")
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name="id", value="ID")
    public CommonResult<String> delete(@PathVariable Integer id) {
        if (deliverySetTimeService.removeById(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('admin:deliveryTime:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated DeliveryTimeRequest request) {
        DeliverySetTime entity = new DeliverySetTime();
        BeanUtils.copyProperties(request, entity);
        if (entity.getType() == 1) {
            entity.setStoreId(0);
        }
        if (deliverySetTimeService.updateById(entity)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('admin:deliveryTime:info')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name="id", value="ID")
    public CommonResult<DeliverySetTime> info(@PathVariable Integer id) {
        return CommonResult.success(deliverySetTimeService.getById(id));
   }
}



