package com.zfire.mall.manager.controller.engin.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ApiNotAuth;
import com.zfire.mall.manager.bean.engin.order.EnginOrderListBean;
import com.zfire.mall.manager.datasource.engin.entity.AgencyBills;
import com.zfire.mall.manager.datasource.engin.entity.AgencyBillsBean;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.engin.order.EnginSysLogic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

@Slf4j
@RestController
@Api(value = "工程系统接口API", tags ={"工程系统接口API"} )
@RequestMapping(value = "/engin-sys/api", produces = "application/json; charset=utf-8")
public class EnginSysController {

    @Resource
    EnginSysLogic enginSysLogic;

    @ApiOperation(value = "工程订单列表")
    @GetMapping("/list")
    public ResponseHelper<IPage<AgencyBills>> list(
            @ApiParam(value = "工程登录编号") @RequestParam(required = false) String recordNo,
            @ApiParam(value = "工程项目") @RequestParam(required = false) String projectName,
            @ApiParam(value = "使用单位") @RequestParam(required = false) String useUnit,
            @ApiParam(value = "页号",required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true)@RequestParam Integer pageSize
    ) throws Exception {
        IPage<AgencyBills> listPage = enginSysLogic.listPage(recordNo, projectName, useUnit, pageNum, pageSize);
        return ResponseHelper.success(listPage);
    }

    @ApiOperation(value = "工程订单详情")
    @GetMapping("/detail")
    public ResponseHelper<AgencyBillsBean> detail(
            @ApiParam(value = "销售类型") @NotBlank(message = "销售类型不能为空") @RequestParam String saleTypeId,
            @ApiParam(value = "工程登录编号") @RequestParam(required = false) String recordNo,
            @ApiParam(value = "工程登录用户编号") @RequestParam(required = false) String userId,
            @ApiParam(value = "家用工程订单号") @RequestParam(required = false) String enginOrderId,
            @ApiParam(value = "经销商id") @RequestParam(required = false) String customerId
    ) throws Exception {
        AgencyBillsBean bills = enginSysLogic.findBills(saleTypeId, recordNo, userId, enginOrderId, customerId);
        return ResponseHelper.success(bills);
    }

    @ApiNotAuth
    @ApiOperation(value = "平台查询工程登录单列表")
    @PostMapping("/platform/query/list")
    public ResponseHelper<IPage<EnginOrderListBean>> platformQueryList(
            @ApiParam(value = "帐号",required = true) @RequestParam String userName,
            @ApiParam(value = "密码",required = true) @RequestParam String password,
            @ApiParam(value = "时间戳",required = true) @RequestParam String timestamp,
            @ApiParam(value = "签名",required = true) @RequestParam String sign,
            @ApiParam(value = "工程登录编号") @RequestParam(required = false) String recordNo,
            @ApiParam(value = "订单编号") @RequestParam(required = false) String orderNo,
            @ApiParam(value = "使用单位") @RequestParam(required = false) String useUnit,
            @ApiParam(value = "规格机型") @RequestParam(required = false) String specs,
            @ApiParam(value = "实装机型") @RequestParam(required = false) String realModel,
            @ApiParam(value = "客户地址") @RequestParam(required = false) String address,
            @ApiParam(value = "单据类型") @RequestParam(required = false) String orderType,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws Exception {
        IPage<EnginOrderListBean> listPage = enginSysLogic.platformQueryList(userName, password, timestamp, sign, recordNo,
                orderNo, useUnit, specs, realModel, address, orderType, pageNum, pageSize);
        return ResponseHelper.success(listPage);
    }

    @ApiNotAuth
    @ApiOperation(value = "平台引用工程登录单")
    @PostMapping("/platform/ref")
    public ResponseHelper platformRef(
            @ApiParam(value = "工程登录编号",required = true) @RequestParam String recordNo,
            @ApiParam(value = "id",required = true) @RequestParam String itemId,
            @ApiParam(value = "引用状态 true=是 false=否",required = true) @RequestParam Boolean isRef,
            @ApiParam(value = "时间戳",required = true) @RequestParam String timestamp,
            @ApiParam(value = "签名",required = true) @RequestParam String sign
    ) throws Exception {
        enginSysLogic.platformRef(recordNo, itemId, isRef, timestamp, sign);
        return ResponseHelper.success();
    }
}
