package shop.alien.second.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.second.SecondEntrustUser;
import shop.alien.entity.second.vo.SecondEntrustUserDTO;
import shop.alien.entity.second.vo.SecondEntrustUserDetailVo;
import shop.alien.entity.second.vo.SecondEntrustUserQueryVo;
import shop.alien.entity.second.vo.SecondEntrustUserResultVo;
import shop.alien.second.service.SecondEntrustUserService;

import java.util.List;

/**
 * <p>
 * 二手委托人信息表 前端控制器
 * </p>
 *
 * @author ssk
 * @since 2025-11-21
 */
@Slf4j
@Api(tags = {"二手平台-委托人信息管理"})
@ApiSort(10)
@CrossOrigin
@RequiredArgsConstructor
@RestController
@RequestMapping("/secondEntrustUser")
public class SecondEntrustUserController {

    private final SecondEntrustUserService secondEntrustUserService;

    @ApiOperation("创建委托人信息")
    @ApiOperationSupport(order = 1)
    @PostMapping("/create")
    public R<Boolean> createEntrustUser(@Validated @RequestBody SecondEntrustUserDTO dto) {
        log.info("SecondEntrustUserController.createEntrustUser dto={}", dto);
        try {
            boolean result = secondEntrustUserService.createEntrustUser(dto);
            if (result) {
                return R.success("创建成功");
            }
            return R.fail("创建失败");
        } catch (Exception e) {
            log.error("SecondEntrustUserController.createEntrustUser error: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("根据交易ID获取委托人信息")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entrustTradeId", value = "交易ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getByTradeId")
    public R<SecondEntrustUser> getByTradeId(@RequestParam Integer entrustTradeId) {
        log.info("SecondEntrustUserController.getByTradeId entrustTradeId={}", entrustTradeId);
        try {
            SecondEntrustUser entrustUser = secondEntrustUserService.getByTradeId(entrustTradeId);
            return R.data(entrustUser);
        } catch (Exception e) {
            log.error("SecondEntrustUserController.getByTradeId error: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("根据交易编号获取委托人信息")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entrustTradeNo", value = "交易编号", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getByTradeNo")
    public R<SecondEntrustUser> getByTradeNo(@RequestParam String entrustTradeNo) {
        log.info("SecondEntrustUserController.getByTradeNo entrustTradeNo={}", entrustTradeNo);
        try {
            SecondEntrustUser entrustUser = secondEntrustUserService.getByTradeNo(entrustTradeNo);
            return R.data(entrustUser);
        } catch (Exception e) {
            log.error("SecondEntrustUserController.getByTradeNo error: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("根据ID获取委托人信息")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "委托人ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getById")
    public R<SecondEntrustUser> getById(@RequestParam Integer id) {
        log.info("SecondEntrustUserController.getById id={}", id);
        try {
            SecondEntrustUser entrustUser = secondEntrustUserService.getById(id);
            if (entrustUser == null) {
                return R.fail("委托人信息不存在");
            }
            return R.data(entrustUser);
        } catch (Exception e) {
            log.error("SecondEntrustUserController.getById error: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("更新委托人信息")
    @ApiOperationSupport(order = 5)
    @PutMapping("/update/{id}")
    public R<Boolean> updateEntrustUser(
            @ApiParam(value = "委托人ID", required = true) @PathVariable Integer id,
            @Validated @RequestBody SecondEntrustUserDTO dto) {
        log.info("SecondEntrustUserController.updateEntrustUser id={}, dto={}", id, dto);
        try {
            boolean result = secondEntrustUserService.updateEntrustUser(id, dto);
            if (result) {
                return R.success("更新成功");
            }
            return R.fail("更新失败");
        } catch (Exception e) {
            log.error("SecondEntrustUserController.updateEntrustUser error: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("删除委托人信息")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "委托人ID", dataType = "Integer", paramType = "query", required = true)
    })
    @DeleteMapping("/delete")
    public R<Boolean> deleteEntrustUser(@RequestParam Integer id) {
        log.info("SecondEntrustUserController.deleteEntrustUser id={}", id);
        try {
            boolean result = secondEntrustUserService.deleteEntrustUser(id);
            if (result) {
                return R.success("删除成功");
            }
            return R.fail("删除失败");
        } catch (Exception e) {
            log.error("SecondEntrustUserController.deleteEntrustUser error: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("根据用户电话查询委托人信息列表")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entrustUserPhone", value = "用户电话", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getByUserPhone")
    public R<List<SecondEntrustUser>> getByUserPhone(@RequestParam String entrustUserPhone) {
        log.info("SecondEntrustUserController.getByUserPhone entrustUserPhone={}", entrustUserPhone);
        try {
            List<SecondEntrustUser> list = secondEntrustUserService.getByUserPhone(entrustUserPhone);
            return R.data(list);
        } catch (Exception e) {
            log.error("SecondEntrustUserController.getByUserPhone error: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("分页查询委托人信息列表")
    @ApiOperationSupport(order = 7)
    @PostMapping("/page")
    public R<IPage<SecondEntrustUserResultVo>> getEntrustUserPage(@RequestBody SecondEntrustUserQueryVo queryVo) {
        log.info("SecondEntrustUserController.getEntrustUserPage queryVo={}", queryVo);
        try {
            // 设置默认分页参数
            if (queryVo.getPageNum() == null || queryVo.getPageNum() < 1) {
                queryVo.setPageNum(1);
            }
            if (queryVo.getPageSize() == null || queryVo.getPageSize() < 1) {
                queryVo.setPageSize(10);
            }
            
            IPage<SecondEntrustUserResultVo> page = secondEntrustUserService.getEntrustUserPage(queryVo);
            return R.data(page);
        } catch (Exception e) {
            log.error("SecondEntrustUserController.getEntrustUserPage error: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("根据姓名和身份证号获取委托人详情")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "entrustUserName", value = "委托人姓名", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "entrustIdCard", value = "委托人身份证号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/detail")
    public R<SecondEntrustUserDetailVo> getEntrustUserDetail(@RequestParam String entrustUserName, @RequestParam String entrustIdCard, @RequestParam Integer id) {
        log.info("SecondEntrustUserController.getEntrustUserDetail entrustUserName={}, entrustIdCard={},id={}", entrustUserName, entrustIdCard,id);
        try {
            SecondEntrustUserDetailVo detailVo = secondEntrustUserService.getEntrustUserDetail(entrustUserName, entrustIdCard,id);
            return R.data(detailVo);
        } catch (Exception e) {
            log.error("SecondEntrustUserController.getEntrustUserDetail error: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

}

