package com.sqx.modules.bl.commission.jjr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.bl.commission.jjr.entity.JjrConfig;
import com.sqx.modules.bl.commission.jjr.service.JjrApplyService;
import com.sqx.modules.bl.commission.jjr.service.JjrConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/commission/jjrConfig")
@Api(value = "分佣管理", tags = {"经纪人配置"})
public class JjrConfigController {

    @Autowired
    private JjrConfigService service;
    @Autowired
    private JjrApplyService applyService;

    @GetMapping("/info")
    @ApiOperation("查询经纪人配置")
    public Result info(JjrConfig jjrConfig){
        JjrConfig entity = service.getById(jjrConfig);
        if(entity == null){
            entity = service.getOne(new QueryWrapper<JjrConfig>());
        }
        return Result.success().put("data",entity);
    }

    @PostMapping("/save")
    @ApiOperation("添加经纪人")
    public Result save(JjrConfig jjrConfig){
        JjrConfig entity = service.getById(jjrConfig);
        if(entity!=null){
            service.updateById(jjrConfig);
        }else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            jjrConfig.setCreateTime(sdf.format(new Date()));
            service.save(jjrConfig);
        }
        applyService.setJjrRate(null);
        return Result.success();
    }
}
