package com.nantian.cloud.dbcapp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.dbcapp.entity.TApplyRecord;
import com.nantian.cloud.dbcapp.service.IUpdateConfigService;
import com.nantian.cloud.dbcapp.service.TApplyRecordService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/applyRecord")
@Api(value = "enterprise", tags = "企业端接口")
public class TApplyRecordController {
    private TApplyRecordService tApplyRecordService;

    private IUpdateConfigService updateConfigService;

    @PostMapping("/addApplyRecord")
    @SysLog("新增企业修改数据申请表")
    @ApiOperation(value = "新增企业修改数据申请表", notes = "新增企业修改数据申请表")
    @ApiImplicitParam(name = "TApplyRecord", value = "新增企业修改数据申请表", required = true, dataType = "json", paramType = "path")
    public R save(@Valid @RequestBody TApplyRecord tApplyRecord) {
        if(Double.parseDouble(tApplyRecord.getEditDataValue()) == Double.parseDouble(tApplyRecord.getDataValue())
            && Double.parseDouble(tApplyRecord.getEditConvertRation()) == Double.parseDouble(tApplyRecord.getConvertRation())
        ){
            return R.failed("数据修改申请提交失败，修改的数值不能与原始数据相同！");
        }
        if (updateConfigService.isCheckUpdateConfig(tApplyRecord.getEnterpriseCode(),tApplyRecord.getStatDate())){
            return R.failed("当前数据上传时间为：" + tApplyRecord.getStatDate() + ",已超出企业修改数据申请时间范围，无法提交申请！");
        }
        //加一个，不能重复申请修改
        boolean state=tApplyRecordService.insertInfo(tApplyRecord);
        return state ? R.ok() : R.failed();
    }

    @ApiOperation(value = "修改企业修改数据申请", notes = "修改企业修改数据申请")
    @PostMapping("/updateApplyRecord")
    @SysLog("修改企业修改数据申请")
    public R updateCorrect(@RequestBody TApplyRecord tApplyRecord){
        boolean state=tApplyRecordService.updateRecordById(tApplyRecord);
        return state ? R.ok() : R.failed();
    }

    @GetMapping("/getRecordList")
    @SysLog("获取企业修改数据申请")
    @ApiOperation(value = "获取企业修改数据申请", notes = "获取企业修改数据申请")
    public R getList(Page page, TApplyRecord tApplyRecord){
        return R.ok(tApplyRecordService.getListByPage(page,tApplyRecord));
    }

    @GetMapping("/getRecordPage")
    @SysLog("获取企业修改数据申请")
    @ApiOperation(value = "获取企业修改数据申请", notes = "获取企业修改数据申请")
    public R getRecordPage(Page page, TApplyRecord tApplyRecord){
        return R.ok(tApplyRecordService.getRecordPage(page,tApplyRecord));
    }
}
