package com.ics.controller.mobile;

import cn.hutool.core.util.ObjectUtil;
import com.ics.admin.domain.ApplySettle;
import com.ics.admin.domain.Park;
import com.ics.admin.service.IParkService;
import com.ics.common.utils.ValidatorUtils;
import com.ics.service.IApplySettleService;
import com.ics.common.core.controller.BaseController;
import com.ics.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wf.jwtp.annotation.RequiresPermissions;

/**
 * 入驻申请 提供者
 *
 * @author zzm
 * @date 2021-04-12
 */
@RestController
@RequestMapping("/settle")
public class ApplySettleAPIController extends BaseController {

    @Autowired
    private IApplySettleService applySettleService;

    @Autowired
    private IParkService parkService;


    @RequiresPermissions("member:center:view")
    @GetMapping("get/{id}")
    public R get(@PathVariable("id") Long id) {
        return R.data(applySettleService.selectApplySettleById(id));
    }

    /**
     * 查询入驻申请
     */
    @RequiresPermissions("member:center:view")
    @GetMapping("getByCurrentUser")
    public R getByCurrentUser() {
        return R.data(applySettleService.selectApplySettleByCurrentUser(getCurrentUserId()));
    }


    /**
     * 新增保存入驻申请
     */
    @RequiresPermissions("member:center:view")
    @PostMapping("save")
    public R addSave(@RequestBody ApplySettle applySettle) {
        ValidatorUtils.validateEntity(applySettle);
        if (applySettle.getParkId() == null) {
            return R.error("请选择园区!");
        }
        applySettle.setStatus(ApplySettle.Status.APPROVING);
        applySettle.setCreateUserId(getCurrentUserId());
        Park park = parkService.selectParkById(applySettle.getParkId());
        if (ObjectUtil.isNotEmpty(park)) {
            applySettle.setTenantId(park.getTenantId());
        }
        return toAjax(applySettleService.insertApplySettle(applySettle));
    }


    /**
     * 修改保存入驻申请
     */
    @RequiresPermissions("member:center:view")
    @PostMapping("update")
    public R editSave(@RequestBody ApplySettle applySettle) {
        return toAjax(applySettleService.updateApplySettle(applySettle));
    }
}
