package com.sunxung.factoring.web.tradingPlatform;

import com.alibaba.fastjson.JSONObject;
import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.component.util.ValidatorUtil;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.ValidatorEntity;
import com.sunxung.factoring.entity.tradingPlatform.Platform;
import com.sunxung.factoring.entity.tradingPlatform.TradingPlatformInvoiceInfo;
import com.sunxung.factoring.entity.vo.ConditionVo;
import com.sunxung.factoring.service.tradingPlatform.PlatformService;
import com.sunxung.factoring.service.tradingPlatform.TradingPlatformInvoiceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 平台模块
 *
 * @author liutao
 */
@Controller
@RequestMapping("platform")
public class PlatformController {

    @Autowired
    private PlatformService platformService;

    @Autowired
    private TradingPlatformInvoiceInfoService tradingPlatformInvoiceInfoService;

    /**
     * 跳转至列表页
     *
     * @return
     */
    @RequestMapping("platformList")
    public String listPage() {
        return ConstantUtil.PLATFORM_PREFIX + "platformList";
    }

    /**
     * 跳转至添加页
     *
     * @return
     */
    @RequestMapping("addPage")
    public String addPage() {
        return ConstantUtil.PLATFORM_PREFIX + "platformAdd";
    }

    /**
     * 新增贸易平台
     * add
     *
     * @param platform 贸易平台信息
     */
    @RequestMapping("add")
    @ResponseBody
    @OperationLog(operationModule = "贸易平台",operationType = OperationTypeEnum.INSERT,description = "新增贸易平台")
    public ResponseJson addPlatform(@RequestBody Platform platform) {
        return platformService.add(platform);
    }

    /**
     * 刷新认证和自动签署链接
     * @param id
     * @return
     */
    @RequestMapping("flushUrl")
    @ResponseBody
    @OperationLog(operationModule = "贸易平台",operationType = OperationTypeEnum.UPDATE,description = "刷新认证和自动签署链接")
    public ResponseJson flushUrl(Long id) {
        platformService.flushUrl(id);
        return new ResponseJson("刷新成功");
    }

    /**
     * 刷新认证状态
     * @param id
     * @return
     */
    @RequestMapping("flush")
    @ResponseBody
    @OperationLog(operationModule = "贸易平台",operationType = OperationTypeEnum.UPDATE,description = "刷新认证")
    public ResponseJson flush(Long id) {
        platformService.flush(id);
        return new ResponseJson("刷新成功");
    }

    /**
     * 查询所有贸易平台
     * findAll
     *
     * @return 所有贸易平台
     */
    @RequestMapping("list")
    @ResponseBody
    public ResponseJson findAll() {
        return new ResponseJson(platformService.findAll());
    }


    /**
     * 查询所有贸易平台分页
     * findPage
     *
     * @return 所有贸易平台
     */
    @RequestMapping("findPage")
    @ResponseBody
    public GridPage findPage(ConditionVo conditionVo) {
        return platformService.findPage(conditionVo);
    }

    /**
     * 确定业务获取贸易平台
     *
     * @param businessNumber
     * @return
     */
    @RequestMapping("getPlateformByBusiness")
    @ResponseBody
    public ResponseJson getPlateformByBusiness(String businessNumber) {
        return new ResponseJson(platformService.getPlateformByBusiness(businessNumber));
    }

    /**
     * 跳转到开票信息提交页面
     *
     * @param id
     * @return
     */
    @RequestMapping("/editInvoice")
    @ResponseBody
    public ResponseJson editInvoice(Long id) {
        Platform platform = platformService.getById(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("platformInfo", platform);
        jsonObject.put("platformInvoice", tradingPlatformInvoiceInfoService.findByPlatformId(id));
        return new ResponseJson(jsonObject);
    }

    /**
     * 开票信息提交接口
     *
     * @param invoiceInfo
     * @return
     */
    @PostMapping("/doEditInvoice")
    @ResponseBody
    @OperationLog(operationModule = "贸易平台",operationType = OperationTypeEnum.INSERT,description = "开票信息提交接口")
    public ResponseJson doEditInvoice(@RequestBody TradingPlatformInvoiceInfo invoiceInfo) {
        ValidatorUtil.validator(ValidatorEntity.ValidatorType.ADD, invoiceInfo);
        tradingPlatformInvoiceInfoService.saveOrUpdate(invoiceInfo);
        return new ResponseJson("操作成功！");
    }


}
