package com.jeeplus.psimanage.dishManage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import com.jeeplus.psimanage.dishManage.domain.PsiCreditCustomer;
import com.jeeplus.psimanage.dishManage.service.PsiCreditCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 挂账人信息登记
 */
@RestController
@Api(tags = "挂账人信息登记")
@RequestMapping(value = "/psi/creditCustomer")
public class PsiCreditCustomerController {

    @Resource
    private PsiCreditCustomerService service;

    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    public ResponseEntity<IPage<PsiCreditCustomer>> list(Page<PsiCreditCustomer> page, PsiCreditCustomer info) {
        return ResponseEntity.ok(service.list(page, info));
    }

    @ApiOperation(value = "查询详情")
    @GetMapping("/findById")
    public ResponseEntity<PsiCreditCustomer> findById(@RequestParam String id) {
        return ResponseEntity.ok(service.findById(id));
    }

    @ApiLog(value = "保存挂账人", type = LogTypeEnum.SAVE)
    @ApiOperation(value = "新增/修改")
    @PreAuthorize("hasAnyAuthority('psi:creditCustomer:add','psi:creditCustomer:edit')")
    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody PsiCreditCustomer info) {
        return ResponseEntity.ok(service.saveCreditCustomer(info));
    }

    @ApiLog(value = "保存挂账人流程表单", type = LogTypeEnum.SAVE)
    @ApiOperation(value = "新增/修改流程表单")
    @PreAuthorize("hasAnyAuthority('psi:creditCustomer:add','psi:creditCustomer:edit')")
    @PostMapping("/saveFlow")
    public ResponseEntity<String> saveFlow(@RequestBody PsiCreditCustomer info) {
        String id = service.saveCreditCustomerForFlow(info);
        return ResponseUtil.newInstance()
                .add("businessTable", "psi_credit_customer")
                .add("businessId", id)
                .ok("操作成功");
    }

    @ApiOperation(value = "根据id修改审核状态")
    @PreAuthorize("hasAnyAuthority('psi:creditCustomer:add','psi:creditCustomer:edit')")
    @PostMapping("/updateStatusById")
    public void updateStatusById(@RequestBody PsiCreditCustomer info) {
        service.updateStatusById(info);
    }

    @ApiLog(value = "删除挂账人", type = LogTypeEnum.SAVE)
    @ApiOperation(value = "删除")
    @PreAuthorize("hasAuthority('psi:creditCustomer:del')")
    @DeleteMapping("/delete")
    public ResponseEntity<String> delete(@RequestParam String ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }

    @ApiLog(value = "修改挂账状态", type = LogTypeEnum.SAVE)
    @ApiOperation(value = "修改挂账状态")
    @PreAuthorize("hasAuthority('psi:creditCustomer:edit')")
    @GetMapping("/changeStatus")
    public ResponseEntity<String> changeStatus(@RequestParam String id, @RequestParam String creditStatus) {
        return ResponseEntity.ok(service.changeStatus(id, creditStatus));
    }
}
