package com.zt.plat.module.contractorder.controller.admin.contractreceivesend;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.contractorder.api.vo.contract.ContractReceiveSendPageReqVO;
import com.zt.plat.module.contractorder.api.vo.contract.ContractReceiveSendRespVO;
import com.zt.plat.module.contractorder.api.vo.contract.ContractReceiveSendSaveReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.contractreceivesend.ContractReceiveSendDO;
import com.zt.plat.module.contractorder.service.contractreceivesend.ContractReceiveSendService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 收发货规则")
@RestController
@RequestMapping("/base/contract-receive-send")
@Validated
public class ContractReceiveSendController implements BusinessControllerMarker {


    @Resource
    private ContractReceiveSendService contractReceiveSendService;

    @PostMapping("/create")
    @Operation(summary = "创建收发货规则")
    @PreAuthorize("@ss.hasPermission('base:contract-receive-send:create')")
    public CommonResult<ContractReceiveSendRespVO> createContractReceiveSend(@Valid @RequestBody ContractReceiveSendSaveReqVO createReqVO) {
        return success(contractReceiveSendService.createContractReceiveSend(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收发货规则")
    @PreAuthorize("@ss.hasPermission('base:contract-receive-send:update')")
    public CommonResult<Boolean> updateContractReceiveSend(@Valid @RequestBody ContractReceiveSendSaveReqVO updateReqVO) {
        contractReceiveSendService.updateContractReceiveSend(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收发货规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:contract-receive-send:delete')")
    public CommonResult<Boolean> deleteContractReceiveSend(@RequestParam("id") Long id) {
        contractReceiveSendService.deleteContractReceiveSend(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除收发货规则")
                @PreAuthorize("@ss.hasPermission('base:contract-receive-send:delete')")
    public CommonResult<Boolean> deleteContractReceiveSendList(@RequestBody BatchDeleteReqVO req) {
        contractReceiveSendService.deleteContractReceiveSendListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收发货规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:contract-receive-send:query')")
    public CommonResult<ContractReceiveSendRespVO> getContractReceiveSend(@RequestParam("id") Long id) {
        ContractReceiveSendDO contractReceiveSend = contractReceiveSendService.getContractReceiveSend(id);
        return success(BeanUtils.toBean(contractReceiveSend, ContractReceiveSendRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收发货规则分页")
    @PreAuthorize("@ss.hasPermission('base:contract-receive-send:query')")
    public CommonResult<PageResult<ContractReceiveSendRespVO>> getContractReceiveSendPage(@Valid ContractReceiveSendPageReqVO pageReqVO) {
        PageResult<ContractReceiveSendDO> pageResult = contractReceiveSendService.getContractReceiveSendPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContractReceiveSendRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收发货规则 Excel")
    @PreAuthorize("@ss.hasPermission('base:contract-receive-send:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportContractReceiveSendExcel(@Valid ContractReceiveSendPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContractReceiveSendDO> list = contractReceiveSendService.getContractReceiveSendPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收发货规则.xls", "数据", ContractReceiveSendRespVO.class,
                        BeanUtils.toBean(list, ContractReceiveSendRespVO.class));
    }

}