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

import com.unis.framework.apilog.core.annotation.ApiAccessLog;
import com.unis.framework.common.pojo.CommonResult;
import com.unis.framework.common.pojo.PageParam;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.util.kqbm.KQBMUtils;
import com.unis.framework.common.util.object.BeanUtils;
import com.unis.framework.excel.core.util.ExcelUtils;
import com.unis.module.coding.controller.admin.lqjcxx.vo.LqjcxxPageReqVO;
import com.unis.module.coding.controller.admin.lqjcxx.vo.LqjcxxRespVO;
import com.unis.module.coding.controller.admin.lqjcxx.vo.LqjcxxSaveReqVO;
import com.unis.module.coding.dal.dataobject.lqjcxx.LqjcxxDO;
import com.unis.module.coding.service.lqjcxx.LqjcxxService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

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

@Tag(name = "管理后台 - 粮情检测信息")
@RestController
@RequestMapping("/delivery/lqjcxx")
@Validated
public class LqjcxxController {

    @Resource
    private LqjcxxService lqjcxxService;

    @PostMapping("/create")
    @Operation(summary = "创建粮情检测信息")
    @PreAuthorize("@ss.hasPermission('delivery:lqjcxx:create')")
    public CommonResult<Long> createLqjcxx(@Valid @RequestBody LqjcxxSaveReqVO createReqVO) {
        return success(lqjcxxService.createLqjcxx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新粮情检测信息")
    @PreAuthorize("@ss.hasPermission('delivery:lqjcxx:update')")
    public CommonResult<Boolean> updateLqjcxx(@Valid @RequestBody LqjcxxSaveReqVO updateReqVO) {
        lqjcxxService.updateLqjcxx(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除粮情检测信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('delivery:lqjcxx:delete')")
    public CommonResult<Boolean> deleteLqjcxx(@RequestParam("id") Long id) {
        lqjcxxService.deleteLqjcxx(id);
        return success(true);
    }

    @GetMapping("/get-by-cfid")
    @Operation(summary = "根据仓房id获得粮情检测信息")
    @Parameter(name = "cfid", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('delivery:lqjcxx:querybycdandhwk')")
    public CommonResult<LqjcxxRespVO> getLqjcxxByCfId(@RequestParam("cfId") Long cfId) {
        LqjcxxDO lqjcxx = lqjcxxService.getLqjcxxByCfId(cfId);
        LqjcxxRespVO lqjcxxRespVO = BeanUtils.toBean(lqjcxx, LqjcxxRespVO.class);
        return success(lqjcxxRespVO);
    }


    @GetMapping("/get")
    @Operation(summary = "获得粮情检测信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('delivery:lqjcxx:query')")
    public CommonResult<LqjcxxRespVO> getLqjcxx(@RequestParam("id") Long id) {
        LqjcxxDO lqjcxx = lqjcxxService.getLqjcxx(id);
        return success(BeanUtils.toBean(lqjcxx, LqjcxxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得粮情检测信息分页")
    @PreAuthorize("@ss.hasPermission('delivery:lqjcxx:query')")
    public CommonResult<PageResult<LqjcxxRespVO>> getLqjcxxPage(@Valid LqjcxxPageReqVO pageReqVO) {
        PageResult<LqjcxxDO> pageResult = lqjcxxService.getLqjcxxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LqjcxxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出粮情检测信息 Excel")
    @PreAuthorize("@ss.hasPermission('delivery:lqjcxx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLqjcxxExcel(@Valid LqjcxxPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LqjcxxDO> list = lqjcxxService.getLqjcxxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "粮情检测信息.xls", "数据", LqjcxxRespVO.class,
                BeanUtils.toBean(list, LqjcxxRespVO.class));
    }

}