package com.unis.module.coding.controller.admin.syxx;

import com.unis.module.coding.controller.admin.zjxx.vo.ZjxxRespVO;
import com.unis.module.coding.dal.dataobject.syxx.SyxxDO;
import com.unis.module.coding.dal.dataobject.zjxx.ZjxxDO;
import com.unis.module.coding.service.syxx.SyxxService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.unis.framework.common.pojo.PageParam;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.pojo.CommonResult;
import com.unis.framework.common.util.object.BeanUtils;
import static com.unis.framework.common.pojo.CommonResult.success;

import com.unis.framework.excel.core.util.ExcelUtils;

import com.unis.framework.apilog.core.annotation.ApiAccessLog;
import static com.unis.framework.apilog.core.enums.OperateTypeEnum.*;

import com.unis.module.coding.controller.admin.syxx.vo.*;


@Tag(name = "管理后台 - 损益信息")
@RestController
@RequestMapping("/delivery/syxx")
@Validated
public class SyxxController {

    @Resource
    private SyxxService syxxService;

    @PostMapping("/create")
    @Operation(summary = "创建损益信息")
    @PreAuthorize("@ss.hasPermission('delivery:syxx:create')")
    public CommonResult<Long> createSyxx(@Valid @RequestBody SyxxSaveReqVO createReqVO) {
        return success(syxxService.createSyxx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新损益信息")
    @PreAuthorize("@ss.hasPermission('delivery:syxx:update')")
    public CommonResult<Boolean> updateSyxx(@Valid @RequestBody SyxxSaveReqVO updateReqVO) {
        syxxService.updateSyxx(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除损益信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('delivery:syxx:delete')")
    public CommonResult<Boolean> deleteSyxx(@RequestParam("id") Long id) {
        syxxService.deleteSyxx(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得损益信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('delivery:syxx:query')")
    public CommonResult<SyxxRespVO> getSyxx(@RequestParam("id") Long id) {
        SyxxDO syxx = syxxService.getSyxx(id);
        return success(BeanUtils.toBean(syxx, SyxxRespVO.class));
    }
    @GetMapping("/get-by-cf")
    @Operation(summary = "根据仓房获得损益信息")
    @Parameter(name = "id", description = "仓房id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('delivery:syxx:querybycf')")
    public CommonResult<SyxxRespVO> getZjxx(@RequestParam("cfId") Long cfId) {
        SyxxDO syxxDO = syxxService.getSyxxByCfId(cfId);
        return success(BeanUtils.toBean(syxxDO, SyxxRespVO.class));
    }
    @GetMapping("/page")
    @Operation(summary = "获得损益信息分页")
    @PreAuthorize("@ss.hasPermission('delivery:syxx:query')")
    public CommonResult<PageResult<SyxxRespVO>> getSyxxPage(@Valid SyxxPageReqVO pageReqVO) {
        PageResult<SyxxDO> pageResult = syxxService.getSyxxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SyxxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出损益信息 Excel")
    @PreAuthorize("@ss.hasPermission('delivery:syxx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSyxxExcel(@Valid SyxxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SyxxDO> list = syxxService.getSyxxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "损益信息.xls", "数据", SyxxRespVO.class,
                        BeanUtils.toBean(list, SyxxRespVO.class));
    }

}