package com.ruoyi.dk.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.component.core.domain.R;
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.Customer;
import com.ruoyi.dk.dto.admin.req.UpdatePwdCustomerReq;
import com.ruoyi.dk.dto.admin.resp.CustomerAdminResp;
import com.ruoyi.dk.dto.common.IdReq;
import com.ruoyi.dk.service.CustomerService;
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.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

    private final CustomerService customerService;

    @SaCheckPermission("dk:dkCustomer:list")
    @GetMapping("/list")
    public TableDataInfo<CustomerAdminResp> list(PageQuery pageQuery, @Validated CustomerAdminResp bo) {
        IPage<CustomerAdminResp> page = customerService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }

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


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

    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable String ids) {
        List<String> idList = Stream.of(ids.split(",")).collect(Collectors.toList());
        return toAjax(customerService.removeByIds(idList) ? 1 : 0);
    }


    @Log(title = "修改密码" , businessType = BusinessType.DELETE)
    @PostMapping("/resetPwd")
    public R<Void> resetPwd(@RequestBody UpdatePwdCustomerReq customer) {
        return toAjax(customerService.updatePwd(customer.getCustomerId(),customer.getPassword(),true));
    }

    @Log(title = "强制T人" , businessType = BusinessType.DELETE)
    @PostMapping("/loginOut")
    public R<Void> loginOut(@RequestBody IdReq id) {
        customerService.loginOut(id.getId());
        return R.ok();
    }
}
