package com.yc.ship.module.resource.controller.admin.company;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.resource.controller.admin.company.vo.CompanyPageReqVO;
import com.yc.ship.module.resource.controller.admin.company.vo.CompanyRespVO;
import com.yc.ship.module.resource.controller.admin.company.vo.CompanySaveReqVO;
import com.yc.ship.module.resource.controller.admin.company.vo.CompanySimpleRespVO;
import com.yc.ship.module.resource.controller.admin.vo.SimpleListReqVO;
import com.yc.ship.module.resource.dal.dataobject.company.CompanyDO;
import com.yc.ship.module.resource.service.company.CompanyService;
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.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 供应商")
@RestController
@RequestMapping("/resource/company")
@Validated
public class CompanyController {

    @Resource
    private CompanyService companyService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商")
    @PreAuthorize("@ss.hasPermission('dispatch:company:create')")
    public CommonResult<String> createCompany(@Valid @RequestBody CompanySaveReqVO createReqVO) {
        return success(companyService.createCompany(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商")
    @PreAuthorize("@ss.hasPermission('dispatch:company:update')")
    public CommonResult<Boolean> updateCompany(@Valid @RequestBody CompanySaveReqVO updateReqVO) {
        companyService.updateCompany(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dispatch:company:delete')")
    public CommonResult<Boolean> deleteCompany(@RequestParam("id") String id) {
        companyService.deleteCompany(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dispatch:company:query')")
    public CommonResult<CompanyRespVO> getCompany(@RequestParam("id") String id) {
        CompanyDO company = companyService.getCompany(id);
        CompanyRespVO companyRespVO = BeanUtils.toBean(company, CompanyRespVO.class);
        return success(companyRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商分页")
//    @PreAuthorize("@ss.hasPermission('dispatch:company:query')")
    public CommonResult<PageResult<CompanyRespVO>> getCompanyPage(@Valid CompanyPageReqVO pageReqVO) {
        PageResult<CompanyDO> pageResult = companyService.getCompanyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商 Excel")
    @PreAuthorize("@ss.hasPermission('dispatch:company:export')")
    @OperateLog(type = EXPORT)
    public void exportCompanyExcel(@Valid CompanyPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CompanyDO> list = companyService.getCompanyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供应商.xls", "数据", CompanyRespVO.class,
                BeanUtils.toBean(list, CompanyRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取供应商精简信息列表", description = "列表数据")
    public CommonResult<List<CompanySimpleRespVO>> getSimpleCarTypeList(SimpleListReqVO listReqVO) {
        List<CompanyDO> list = companyService.getCompanyList(listReqVO);
        return success(BeanUtils.toBean(list, CompanySimpleRespVO.class));
    }
}