package com.ruoyi.dk.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.component.idempotent.annotation.RepeatSubmit;
import com.ruoyi.component.log.annotation.Log;
import com.ruoyi.component.log.enums.BusinessType;
import com.ruoyi.component.mybatis.core.page.PageQuery;
import com.ruoyi.component.mybatis.core.page.TableDataInfo;
import com.ruoyi.component.web.core.BaseController;
import com.ruoyi.dk.domain.CustomerInfo;
import com.ruoyi.dk.service.CustomerInfoService;
import com.ruoyi.component.core.domain.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/dk/dkCustomerInfo")
public class DkCustomerInfoController extends BaseController {

    private final CustomerInfoService customerInfoService;

    @SaCheckPermission("dk:dkCustomerInfo:list")
    @GetMapping("/list")
    public TableDataInfo<CustomerInfo> list(PageQuery pageQuery, @Validated CustomerInfo bo) {
        IPage<CustomerInfo> page = customerInfoService.page(pageQuery.build(), Wrappers.query(bo));
        return TableDataInfo.build(page);
    }

    @GetMapping("/getInfoByCustomerId")
    public R<CustomerInfo> getInfoByCustomer(Long customerId) {
        return R.ok(customerInfoService.getByCustomerId(customerId));
    }

    @PostMapping("/updateAllowSignature")
    public R<Void> updateAllowSignature(@RequestBody CustomerInfo bo) {
        customerInfoService.updateAllowSignature(bo);
        return R.ok();
    }

    @SaCheckPermission("dk:dkCustomerInfo:query")
    @GetMapping("/{id}")
    public R<CustomerInfo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(customerInfoService.getById(id));
    }

    @SaCheckPermission("dk:dkCustomerInfo:edit")
    @Log(title = "客户资料", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated @RequestBody CustomerInfo bo) {
        return toAjax(customerInfoService.updateById(bo) ? 1 : 0);
    }

}
