package com.gxwebsoft.credit.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.credit.service.CreditMpCustomerService;
import com.gxwebsoft.credit.entity.CreditMpCustomer;
import com.gxwebsoft.credit.param.CreditMpCustomerParam;
import com.gxwebsoft.credit.param.BatchFollowStepApprovalDTO;
import com.gxwebsoft.credit.param.EndFollowProcessDTO;
import com.gxwebsoft.credit.param.FollowStepApprovalDTO;
import com.gxwebsoft.credit.param.FollowStepQueryDTO;
import com.gxwebsoft.credit.vo.FollowStatisticsDTO;
import com.gxwebsoft.credit.vo.PendingApprovalStepVO;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小程序端客户控制器
 *
 * @author 科技小王子
 * @since 2026-03-16 20:59:17
 */
@Tag(name = "小程序端客户管理")
@RestController
@RequestMapping("/api/credit/credit-mp-customer")
public class CreditMpCustomerController extends BaseController {
    @Resource
    private CreditMpCustomerService creditMpCustomerService;

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:list')")
    @Operation(summary = "分页查询小程序端客户")
    @GetMapping("/page")
    public ApiResult<PageResult<CreditMpCustomer>> page(CreditMpCustomerParam param) {
        // 使用关联查询
        return success(creditMpCustomerService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:list')")
    @Operation(summary = "查询全部小程序端客户")
    @GetMapping()
    public ApiResult<List<CreditMpCustomer>> list(CreditMpCustomerParam param) {
        // 使用关联查询
        return success(creditMpCustomerService.listRel(param));
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:list')")
    @Operation(summary = "根据id查询小程序端客户")
    @GetMapping("/{id}")
    public ApiResult<CreditMpCustomer> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(creditMpCustomerService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:save')")
    @OperationLog
    @Operation(summary = "添加小程序端客户")
    @PostMapping()
    public ApiResult<?> save(@RequestBody CreditMpCustomer creditMpCustomer) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         creditMpCustomer.setUserId(loginUser.getUserId());
        }
        if (creditMpCustomerService.save(creditMpCustomer)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:update')")
    @OperationLog
    @Operation(summary = "修改小程序端客户")
    @PutMapping()
    public ApiResult<?> update(@RequestBody CreditMpCustomer creditMpCustomer) {
        if (creditMpCustomerService.updateById(creditMpCustomer)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:remove')")
    @OperationLog
    @Operation(summary = "删除小程序端客户")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (creditMpCustomerService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:save')")
    @OperationLog
    @Operation(summary = "批量添加小程序端客户")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<CreditMpCustomer> list) {
        if (creditMpCustomerService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:update')")
    @OperationLog
    @Operation(summary = "批量修改小程序端客户")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<CreditMpCustomer> batchParam) {
        if (batchParam.update(creditMpCustomerService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:remove')")
    @OperationLog
    @Operation(summary = "批量删除小程序端客户")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (creditMpCustomerService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:update')")
    @OperationLog
    @Operation(summary = "审核跟进步骤")
    @PostMapping("/approve-follow-step")
    public ApiResult<?> approveFollowStep(@RequestBody FollowStepApprovalDTO dto) {
        creditMpCustomerService.approveFollowStep(dto);
        return success("审核成功");
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:update')")
    @OperationLog
    @Operation(summary = "批量审核跟进步骤")
    @PostMapping("/batch-approve-follow-steps")
    public ApiResult<?> batchApproveFollowSteps(@RequestBody BatchFollowStepApprovalDTO dto) {
        creditMpCustomerService.batchApproveFollowSteps(dto);
        return success("批量审核成功");
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:list')")
    @Operation(summary = "获取待审核的跟进步骤")
    @GetMapping("/pending-approval-steps")
    public ApiResult<List<PendingApprovalStepVO>> getPendingApprovalSteps(FollowStepQueryDTO query) {
        List<PendingApprovalStepVO> list = creditMpCustomerService.getPendingApprovalSteps(query);
        return success(list);
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:list')")
    @Operation(summary = "获取客户跟进统计")
    @GetMapping("/follow-statistics/{customerId}")
    public ApiResult<FollowStatisticsDTO> getFollowStatistics(@PathVariable Long customerId) {
        FollowStatisticsDTO statistics = creditMpCustomerService.getFollowStatistics(customerId);
        return success(statistics);
    }

    @PreAuthorize("hasAuthority('credit:creditMpCustomer:update')")
    @OperationLog
    @Operation(summary = "结束客户跟进流程")
    @PostMapping("/end-follow-process")
    public ApiResult<?> endFollowProcess(@RequestBody EndFollowProcessDTO dto) {
        creditMpCustomerService.endFollowProcess(dto);
        return success("流程结束成功");
    }

}
