package com.yc.ship.module.trade.controller.admin.contract;

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.trade.api.contract.dto.ContractApplyReqDTO;
import com.yc.ship.module.trade.controller.admin.contract.vo.ContractPageReqVO;
import com.yc.ship.module.trade.controller.admin.contract.vo.ContractReqVO;
import com.yc.ship.module.trade.controller.admin.contract.vo.ContractRespVO;
import com.yc.ship.module.trade.dal.dataobject.contract.ContractDO;
import com.yc.ship.module.trade.service.contract.ContractService;
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.API;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


/**
 * @author le'yang
 */
@Tag(name = "管理后台 - 合同")
@RestController
@RequestMapping("/trade/contract")
@Validated
public class ContractController {

    @Resource
    private ContractService contractService;

    @PostMapping( "/apply")
    @Operation(summary = "申请合同")
    @OperateLog(type = API)
    public CommonResult<?> applyContract(@Valid @RequestBody ContractApplyReqDTO reqDTO){
        return contractService.applyContract(reqDTO);
    }

    @PostMapping( "/cancel")
    @Operation(summary = "作废合同")
    @OperateLog(type = API)
    public CommonResult<Boolean> cancel(@Valid @RequestBody ContractReqVO reqVO){
        Boolean res = contractService.cancel(reqVO.getId());
        return success(res);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同")
    @OperateLog(type = API,enable = false)
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:contract:query')")
    public CommonResult<ContractRespVO> getContract(@RequestParam("id") Long id) {
        ContractDO contract = contractService.getContract(id);
        return success(BeanUtils.toBean(contract, ContractRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同分页")
    @PreAuthorize("@ss.hasPermission('trade:contract:query')")
    public CommonResult<PageResult<ContractRespVO>> getContractPage(@Valid ContractPageReqVO pageReqVO) {
        PageResult<ContractDO> pageResult = contractService.getContractPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContractRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合同 Excel")
    @PreAuthorize("@ss.hasPermission('trade:contract:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportContractExcel(@Valid ContractPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContractDO> list = contractService.getContractPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合同.xls", "数据", ContractRespVO.class,
                        BeanUtils.toBean(list, ContractRespVO.class));
    }


    @GetMapping("/queryStatus")
    @Operation(summary = "查询合同状态")
    @OperateLog(type = API)
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:contract:query')")
    public CommonResult<Boolean> queryStatus(@RequestParam("id") Long id) {
         contractService.queryContractStatus(id);
        return success(true);
    }

    @PostMapping( "/sendSms")
    @Operation(summary = "重新发送合同短信")
    @OperateLog(type = API)
    @PreAuthorize("@ss.hasPermission('trade:contract:sendsms')")
    public CommonResult<Boolean> sendContractSms(@Valid @RequestBody ContractReqVO reqVO){
        boolean flag = contractService.sendContractSms(reqVO);
        return success(flag);
    }

}