package io.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.common.constant.Constant;
import io.common.page.PageData;
import io.common.utils.Result;
import io.common.validator.AssertUtils;
import io.common.validator.ValidatorUtils;
import io.common.validator.group.AddGroup;
import io.common.validator.group.DefaultGroup;
import io.common.validator.group.UpdateGroup;
import io.dto.LoginDTO;
import io.modules.item.dao.DoctorsDao;
import io.modules.item.dto.DoctorsDTO;
import io.modules.item.entity.DoctorsEntity;
import io.modules.item.service.DoctorsService;
import io.modules.item.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 医生表
 *
 * @author Mark #
 * @since 1.0.0 2025-02-18
 */
@RestController
@RequestMapping("api/doctors")
@Tag(name = "医生表")
@CrossOrigin
public class DoctorsController {
    @Autowired
    private DoctorsService doctorsService;

    @Value("${upload.url}")
    private String uploadUrl;


    @Autowired
    private DoctorsDao doctorsDao;

    @PostMapping("login")
    @Operation(summary = "医生登录")
    public Result<DoctorsEntity> login(@RequestBody DoctorsDTO dto) {
        //用户登录
        DoctorsEntity map = doctorsService.login(dto);
        return new Result().ok(map);
    }


    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String")
    })
    public Result<PageData<DoctorsDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<DoctorsDTO> page = doctorsService.page(params);

        List<DoctorsDTO> collect = page.getList().stream().map(e -> {
            e.setImg(uploadUrl + e.getImg());
            return e;
        }).collect(Collectors.toList());
        page.setList(collect);
        return new Result<PageData<DoctorsDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<DoctorsDTO> get(@PathVariable("id") Long id) {
        DoctorsDTO data = doctorsService.get(id);
        data.setImg(uploadUrl + data.getImg());
        return new Result<DoctorsDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result save(@RequestBody DoctorsDTO dto) {
        //        查询是否存在账号
        LambdaQueryWrapper<DoctorsEntity> lwq = new LambdaQueryWrapper<>();

        lwq.eq(StrUtil.isNotBlank(dto.getNickName()), DoctorsEntity::getNickName, dto.getNickName());
        if (doctorsDao.exists(lwq)) {
            return new Result().error("账号已经存在~");
        }
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        dto.setImg(dto.getImg().replace(uploadUrl, ""));
        dto.setAptitude(dto.getAptitude().replace(uploadUrl, ""));
        dto.setPassword(DigestUtil.sha256Hex(dto.getPassword()));
        doctorsService.save(dto);
        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result update(@RequestBody DoctorsDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        doctorsService.update(dto);
        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        doctorsService.delete(ids);
        return new Result();
    }
}
