package com.muchinfo.mtp3century.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.muchinfo.mtp3century.service.ICommonService;
import com.muchinfo.mtp3century.service.IForwardOrderService;
import com.muchinfo.mtp3century.service.IPresaleService;
import com.muchinfo.mtp3century.utils.AjaxResult;
import com.muchinfo.mtp3common.enumtype.SysConstants;
import com.muchinfo.mtp3common.vo.PageResult;
import com.muchinfo.mtp3interface.mapper.ISystemmanagerMapper;
import com.muchinfo.mtp3pojos.entity.CptradePresaleapply;
import com.muchinfo.mtp3pojos.entity.Systemmanager;
import com.muchinfo.mtp3pojos.vo.presale.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/presale")
@Api(value = "产能预售管理API", description = "产能预售管理")
public class PresaleController {

    @Resource
    private IPresaleService iPresaleService;
    @Resource
    private IForwardOrderService iForwardOrderService;
    @Resource
    private ISystemmanagerMapper iSystemmanagerMapper;
    @Resource
    private ICommonService iCommonService;

    @RequestMapping(method = RequestMethod.GET, value = "/query")
    @ApiOperation("预售管理--> 预售查询 --> 获取列表")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public PageResult<PresaleQueryResult> query(PresaleQueryParam param, HttpServletRequest request) {
        return iPresaleService.query(param, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/view")
    @ApiOperation("预售管理--> 预售查询 --> 查看详情")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult<PresaleQueryDetailResult> view(String applyid, HttpServletRequest request) {
        return iPresaleService.view(applyid, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/viewPresaleLottery")
    @ApiOperation("预售管理--> 预售查询 --> 中签结果")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult<PresalePresaleLotteryResult> viewPresaleLottery(Long goodsid, HttpServletRequest request) {
        return iPresaleService.viewPresaleLottery(goodsid, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/viewfinsh")
    @ApiOperation("预售管理--> 预售查询 --> 配售结果")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult<PresaleFinishResult> viewfinsh(Long goodsid, HttpServletRequest request) {
        return iPresaleService.viewfinsh(goodsid, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/lottery")
    @ApiOperation("预售管理--> 预售查询 --> 摇号")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult lottery(Long goodsid, Integer lotteryqty, HttpServletRequest request) {
        return iPresaleService.lottery(goodsid, lotteryqty, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/complatesView")
    @ApiOperation("预售管理--> 预售查询 --> 完成")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult<PresaleQueryResult> complatesView(Long goodsid, HttpServletRequest request) {
        return iPresaleService.complatesView(goodsid, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getAccountIds")
    @ApiOperation("预售管理--> 预售查询 --> 完成--> 配售信息新增初始化账号")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult<PresaleFinishInitAddResult> getAccountIds(Long goodsid, HttpServletRequest request) {
        return iPresaleService.getAccountIds(goodsid, request);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/saveComplates")
    @ApiOperation("预售管理--> 预售查询 --> 完成--> 保存")
    @ApiResponse(code = 200, message = "成功", response = PresaleComplatesParam.class)
    public AjaxResult saveComplates(@RequestBody PresaleComplatesParam param, HttpServletRequest request) {
        return iPresaleService.saveComplates(param, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/queryApply")
    @ApiOperation("预售管理--> 预售申请 --> 获取列表")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public PageResult<PresaleApplyResult> queryApply(PresaleApplyParam param, HttpServletRequest request) {
        Systemmanager systemmanager = iSystemmanagerMapper.selectOne(new QueryWrapper<Systemmanager>().eq("LOGINCODE", iCommonService.getUserLoginCode(request)));
        if (systemmanager.getReletype().intValue() != SysConstants.SYS_TYPE_MANAGE) {
            param.setAreauserid(systemmanager.getAreauserid());
        }
        return iPresaleService.queryApply(param, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/queryAuditApply")
    @ApiOperation("预售管理--> 预售初审/复审 --> 获取列表")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public PageResult<PresaleApplyResult> queryAuditApply(PresaleApplyParam param, HttpServletRequest request) {
        return iPresaleService.queryApply(param, request);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/firstsave")
    @ApiOperation("预售管理--> 预售初审 --> 初审")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult firstsave(@RequestBody CptradePresaleapply param, HttpServletRequest request) {
        return iPresaleService.firstsave(param, request);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/secondsave")
    @ApiOperation("预售管理--> 预售初审 --> 复审")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult secondsave(@RequestBody CptradePresaleapply param, HttpServletRequest request) {
        return iPresaleService.secondsave(param, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/applyView")
    @ApiOperation("预售管理--> 预售申请 --> 详情")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult<PresaleApplyResult> applyView(String applyid, HttpServletRequest request) {
        return iPresaleService.applyView(applyid, request);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/save")
    @ApiOperation("预售管理--> 预售申请 --> 新增")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult save(@RequestBody PresaleApplyAddParam param, HttpServletRequest request) {
        return iPresaleService.save(param, request);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/cancel")
    @ApiOperation("预售管理--> 预售申请 --> 撤回")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult cancel(String applyid, HttpServletRequest request) {
        return iPresaleService.cancel(applyid, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/saveInit")
    @ApiOperation("预售管理--> 预售申请 --> 新增初始化市场信息")
    @ApiResponse(code = 200, message = "成功", response = PresaleApplyAddInitResult.class)
    public AjaxResult<PresaleApplyAddInitResult> saveInit(HttpServletRequest request) {
        return iPresaleService.saveInit(request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/querypresaleresult")
    @ApiOperation("预售管理--> 预售结果 --> 获取列表")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public PageResult<ForwardPresaleResult> querypresaleresult(ForwardPresaleParam param, HttpServletRequest request) {
        return iForwardOrderService.querypresaleresult(param, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/querypresaleposition")
    @ApiOperation("预售管理--> 定金持仓 --> 获取列表")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public PageResult<ForwardPresalePositionResult> querypresaleposition(ForwardPresalePositionParam param, HttpServletRequest request) {
        return iForwardOrderService.querypresaleposition(param, request);
    }


    @RequestMapping(method = RequestMethod.GET, value = "/queryForward")
    @ApiOperation("远期订单管理--> 订单申请 --> 获取列表")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public PageResult<ForwardOrderApplyResult> queryForward(ForwardOrderApplyParam param, HttpServletRequest request) {
        Systemmanager systemmanager = iSystemmanagerMapper.selectOne(new QueryWrapper<Systemmanager>().eq("LOGINCODE", iCommonService.getUserLoginCode(request)));
        if (systemmanager.getReletype().intValue() != SysConstants.SYS_TYPE_MANAGE) {
            param.setUserid(systemmanager.getAreauserid());
        }
        return iForwardOrderService.queryForward(param, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/queryAuditForward")
    @ApiOperation("远期订单管理--> 订单初审/复审 --> 获取列表")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public PageResult<ForwardOrderApplyResult> queryAuditForward(ForwardOrderApplyParam param, HttpServletRequest request) {
        return iForwardOrderService.queryForward(param, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/forwardcancel")
    @ApiOperation("远期订单管理--> 订单申请 --> 撤销")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult forwardcancel(String applyid, HttpServletRequest request) {
        return iForwardOrderService.forwardcancel(applyid, request);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/forwardfirstsave")
    @ApiOperation("远期订单管理--> 订单初审 --> 初审")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult forwardfirstsave(String applyid, Integer applystatus, String firstremark, HttpServletRequest request) {
        return iForwardOrderService.forwardfirstsave(applyid, applystatus, firstremark, request);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/forwardsecondsave")
    @ApiOperation("远期订单管理--> 订单初审 --> 复审")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult forwardsecondsave(String applyid, Integer applystatus, String secondremark, HttpServletRequest request) {
        return iForwardOrderService.forwardsecondsave(applyid, applystatus, secondremark, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/forwardView")
    @ApiOperation("远期订单管理--> 订单申请 --> 详情")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult<ForwardOrderApplyResult> forwardView(String applyid, HttpServletRequest request) {
        return iForwardOrderService.forwardView(applyid, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/forwardAddInit")
    @ApiOperation("远期订单管理--> 订单申请 --> 新增--> 初始化市场、商品信息（确定商品后展示其余信息）")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult<ForwardAddMarketAndGoods> forwardAddInit(Long marketid, Long goodid, HttpServletRequest request) {
        return iForwardOrderService.forwardAddInit(marketid, goodid, request);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/forwardAdd")
    @ApiOperation("远期订单管理--> 订单申请 --> 新增")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult forwardAdd(@RequestBody ForwardAddParam param, HttpServletRequest request) {
        return iForwardOrderService.forwardAdd(param, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/queryCancelApply")
    @ApiOperation("远期订单管理--> 注销申请 --> 获取列表")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public PageResult<ForwardCancelApplyResult> queryCancelApply(ForwardCancelApplyParam param, HttpServletRequest request) {
        Systemmanager systemmanager = iSystemmanagerMapper.selectOne(new QueryWrapper<Systemmanager>().eq("LOGINCODE", iCommonService.getUserLoginCode(request)));
        if (systemmanager.getReletype().intValue() != SysConstants.SYS_TYPE_MANAGE) {
            param.setUserid(systemmanager.getAreauserid());
        }
        return iForwardOrderService.queryCancelApply(param, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/queryAuditCancel")
    @ApiOperation("远期订单管理--> 注销初审/复审 --> 获取列表")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public PageResult<ForwardCancelApplyResult> queryAuditCancel(ForwardCancelApplyParam param, HttpServletRequest request) {
        return iForwardOrderService.queryCancelApply(param, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/cancelapply")
    @ApiOperation("远期订单管理--> 注销申请 --> 撤销")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult cancelapply(String cancelid, HttpServletRequest request) {
        return iForwardOrderService.cancelapply(cancelid, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/cancelApplyView")
    @ApiOperation("远期订单管理--> 注销申请 --> 详情")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public AjaxResult<ForwardCancelApplyResult> cancelApplyView(Long cancelid, HttpServletRequest request) {
        return iForwardOrderService.cancelApplyView(cancelid, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/cancelApplyAddInit")
    @ApiOperation("远期订单管理--> 注销申请 --> 新增 --> 获取市场列表信息")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public AjaxResult<ForwardCancelApplyInit> cancelApplyAddInit(Long accountid, HttpServletRequest request) {
        return iForwardOrderService.cancelApplyAddInit(accountid, request);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/cancelApplyAdd")
    @ApiOperation("远期订单管理--> 注销申请 --> 新增")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    public AjaxResult cancelApplyAdd(@RequestBody ForwardCancelAddParam param, HttpServletRequest request) {
        return iForwardOrderService.cancelApplyAdd(param, request);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/queryOrder")
    @ApiOperation("远期订单管理--> 订单查询 --> 获取列表")
    @ApiResponse(code = 200, message = "成功", response = PageResult.class)
    public PageResult<ForwardOrderQueryResult> queryOrder(ForwardOrderQueryParam param, HttpServletRequest request) {
        return iForwardOrderService.queryOrder(param, request);
    }
}
