package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.entity.ExpressTemplateDetail;
import com.gxwebsoft.shop.service.ExpressTemplateDetailService;
import com.gxwebsoft.shop.service.ExpressTemplateService;
import com.gxwebsoft.shop.entity.ExpressTemplate;
import com.gxwebsoft.shop.param.ExpressTemplateParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 运费模板控制器
 *
 * @author 科技小王子
 * @since 2024-11-05 18:03:28
 */
@Api(tags = "运费模板管理")
@RestController
@RequestMapping("/api/shop/express-template")
public class ExpressTemplateController extends BaseController {
    @Resource
    private ExpressTemplateService expressTemplateService;
    @Resource
    private ExpressTemplateDetailService expressTemplateDetailService;

    @ApiOperation("分页查询运费模板")
    @GetMapping("/page")
    public ApiResult<PageResult<ExpressTemplate>> page(ExpressTemplateParam param) {
        // 使用关联查询
        return success(expressTemplateService.pageRel(param));
    }

    @ApiOperation("查询全部运费模板")
    @GetMapping()
    public ApiResult<List<ExpressTemplate>> list(ExpressTemplateParam param) {
        // 使用关联查询
        return success(expressTemplateService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:expressTemplate:list')")
    @ApiOperation("根据id查询运费模板")
    @GetMapping("/{id}")
    public ApiResult<ExpressTemplate> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(expressTemplateService.getByIdRel(id));
    }

    @ApiOperation("添加运费模板")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ExpressTemplate expressTemplate) {
        if (expressTemplateService.save(expressTemplate)) {
            for (ExpressTemplateDetail detail : expressTemplate.getDetailList()) {
                detail.setTemplateId(expressTemplate.getId());
            }
            expressTemplateDetailService.saveBatch(expressTemplate.getDetailList());
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改运费模板")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ExpressTemplate expressTemplate) {
        if (expressTemplateService.updateById(expressTemplate)) {
            expressTemplateDetailService.removeByMap(new HashMap<>() {{
                put("template_id", expressTemplate.getId());
            }});
            for (ExpressTemplateDetail detail : expressTemplate.getDetailList()) {
                detail.setTemplateId(expressTemplate.getId());
            }
            expressTemplateDetailService.saveBatch(expressTemplate.getDetailList());
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除运费模板")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (expressTemplateService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加运费模板")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ExpressTemplate> list) {
        if (expressTemplateService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改运费模板")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ExpressTemplate> batchParam) {
        if (batchParam.update(expressTemplateService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除运费模板")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (expressTemplateService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
