package shop.alien.lawyer.controller;

import com.alibaba.nacos.common.utils.CollectionUtils;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LawyerImg;
import shop.alien.entity.store.vo.LawyerImgTypeVo;
import shop.alien.lawyer.service.LawyerImgService;

import java.util.List;

/**
 * 律师图片Controller
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"律师平台-律师图片"})
@ApiSort(12)
@CrossOrigin
@RestController
@RequestMapping("/lawyer/img")
@RequiredArgsConstructor
public class LawyerImgController {

    private final LawyerImgService lawyerImgService;

    @ApiOperation("获取图片")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lawyerId", value = "律师ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "imgType", value = "图片类型, 0:其他, 1:头像, 2:执业证照片, 3:身份证正面, 4:身份证反面, 5:案例图片, 6:相册图片, 7:资质证书, 8:荣誉证书, 9:工作照片", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getByLawyerId")
    public R<List<LawyerImg>> getByLawyerId(Integer lawyerId, Integer imgType) {
        log.info("LawyerImgController.getByLawyerId?lawyerId={}&imgType={}", lawyerId, imgType);
        return R.data(lawyerImgService.getLawyerImg(lawyerId, imgType));
    }

    @ApiOperation("获取所有图片类型和数量")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "lawyerId", value = "律师ID", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getLawyerImgTypeCount")
    public R<List<LawyerImgTypeVo>> getLawyerImgTypeCount(Integer lawyerId) {
        log.info("LawyerImgController.getLawyerImgTypeCount?lawyerId={}", lawyerId);
        return R.data(lawyerImgService.getLawyerImgTypeCount(lawyerId));
    }

    @ApiOperation("新增或修改图片")
    @ApiOperationSupport(order = 3)
    @PostMapping("/saveOrUpdate")
    public R<String> saveOrUpdate(@RequestBody List<LawyerImg> lawyerImgList) {
        log.info("LawyerImgController.saveOrUpdate?lawyerImgList={}", lawyerImgList);
        if (CollectionUtils.isEmpty(lawyerImgList)) {
            return R.fail("图片列表不能为空");
        }
        Integer id = lawyerImgList.get(0).getId();
        if (lawyerImgService.saveOrUpdateBatch(lawyerImgList)) {
            if (null != id) {
                return R.success("修改成功");
            }
            return R.success("新增成功");
        }
        return R.fail("失败");
    }

    @ApiOperation(value = "删除图片")
    @ApiOperationSupport(order = 4)
    @PostMapping("/delete")
    public R<String> delete(@RequestBody List<Integer> ids) {
        log.info("LawyerImgController.delete?ids={}", ids);
        if (lawyerImgService.removeByIds(ids)) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation("获取图片URL列表")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "律师ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "imgType", value = "图片类型", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getUrl")
    public R<List<String>> getUrl(String id, Integer imgType) {
        log.info("LawyerImgController.getUrl?id={}&imgType={}", id, imgType);
        return R.data(lawyerImgService.getUrl(id, imgType));
    }

    @ApiOperation("通过businessId获取图片")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lawyerId", value = "律师ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "imgType", value = "图片类型, 0:其他, 1:头像, 2:执业证照片, 3:身份证正面, 4:身份证反面, 5:案例图片, 6:相册图片, 7:资质证书, 8:荣誉证书, 9:工作照片", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "businessId", value = "业务ID", dataType = "Integer", paramType = "query", required = true)
    })
    @GetMapping("/getByBusinessId")
    public R<List<LawyerImg>> getByBusinessId(Integer lawyerId, Integer imgType, Integer businessId) {
        log.info("LawyerImgController.getByBusinessId?lawyerId={}&imgType={}&businessId={}", lawyerId, imgType, businessId);
        return R.data(lawyerImgService.getByBusinessId(lawyerId, imgType, businessId));
    }
}

