package fcb.project.manager.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import fcb.project.manager.base.entity.TmOperation;
import fcb.project.manager.base.service.impl.TmOperationServiceImpl;
import fcb.project.manager.base.utils.DataUtils;
import fdage.back.sdk.base.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * 2 * @Author: Abner
 * 3 * @Date: 2021/1/13 14:25
 * 4
 */
@Api(tags = "操作日志相关接口")
@RestController
@RequestMapping("fcb/project/operation")
@Log4j2
public class OperationController {

    @Autowired
    private TmOperationServiceImpl tmOperationService;


    @GetMapping("/queryOrSearchList")
    @ApiOperation(value = "根据条件拉取所有操作记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "时间开始", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "时间截止", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "operatorName", value = "操作人（名称）", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "companyId", value = "所属公司ID", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "query", required = true, dataType = "Long")
    })
    public Result<Object> getHouseList(@RequestParam(required = false) String startTime,
                                       @RequestParam(required = false) String endTime,
                                       @RequestParam(required = false) String operatorName,
                                       @RequestParam(required = false) String companyId,
                                       @RequestParam(required = false) String phone,
                                       @RequestParam() Long pageNum,
                                       @RequestParam() Long pageSize){
        IPage<TmOperation> resultPage = tmOperationService.getOperationList(pageNum ,pageSize ,
                                                                             startTime , endTime ,
                                                                              operatorName , companyId , phone);
        return  Result.success(DataUtils.assembleResult(resultPage.getTotal(), resultPage.getPages(),
                resultPage.getCurrent(), resultPage.getRecords()));
    }


    @PostMapping("/addNew")
    @ApiOperation(value = "新增操作记录-用于四维看看、720微服务调用")
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> addNew(@RequestBody TmOperation tmOperation){

        if(!StringUtils.isNoneBlank(tmOperation.getOperatorName() ,
                tmOperation.getOperatorPhone() , tmOperation.getOperateType() , tmOperation.getOperateContent())){
            return Result.failure("缺失必要参数");
        }
        int insert = tmOperationService.insertNew(tmOperation);
        if(insert != 1){
            return Result.failure("新增失败");
        }
        return Result.success("新增成功");
    }


}
