package com.yuchen.saas.park.service.controller.door;

import com.yuchen.saas.park.api.commons.PageWrapper;
import com.yuchen.saas.park.api.commons.domain.IdRequest;
import com.yuchen.saas.park.api.dto.door.DoorAccessControlCardConditionDTO;
import com.yuchen.saas.park.service.controller.door.util.OrganizationIdExtractor;
import com.yuchen.saas.park.service.mapper.domain.DoorAccessControlCardStateAggregationCondition;
import com.yuchen.saas.park.service.service.domain.*;
import com.yuchen.saas.park.service.service.door.card.DoorAccessControlCardService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("/doorCard")
@RequiredArgsConstructor
public class DoorAccessControlCardController {
    private final DoorAccessControlCardService doorAccessControlCardService;

    @PostMapping("/insert")
    @ApiOperation(value = "新增门卡")
    public R insert(@Valid @RequestBody DoorAccessControlCardInsert o) {
        doorAccessControlCardService.insert(o);
        return R.data(Boolean.TRUE);
    }

    /**
     * 批量新增
     * @param list
     * @return
     */
    @PostMapping("/batchInsert")
    public R batchInsert(@Valid @RequestBody @NotEmpty List<DoorAccessControlCardInsert> list) {
        doorAccessControlCardService.batchInsert(list);
        return R.data(Boolean.TRUE);
    }

    /**
     * 用户批量新增
     * @param o
     * @return
     */
    @PostMapping("/batchInsertByUser")
    public R batchInsertByUser(@Valid @RequestBody DoorAccessControlCardUserBatchInsert o) {
        doorAccessControlCardService.batchInsertByUser(o);
        return R.data(Boolean.TRUE);
    }

    @PostMapping("/update")
    @ApiOperation(value = "编辑门卡")
    public R update(@Valid @RequestBody DoorAccessControlCardUpdate o) {
        doorAccessControlCardService.update(o);
        return R.data(Boolean.TRUE);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除门卡:退卡")
    public R delete(@Valid @RequestBody IdRequest o) {
        doorAccessControlCardService.delete(o.getId());
        return R.data(Boolean.TRUE);
    }

    @PostMapping("/batchDelete")
    public R batchDelete(@Valid @RequestBody List<@Positive Long> ids) {
        doorAccessControlCardService.batchDelete(ids);
        return R.data(Boolean.TRUE);
    }

    @PostMapping("/rebind")
    @ApiOperation(value = "换卡")
    public R rebind(@Valid @RequestBody DoorAccessControlCardRebind o) {
        doorAccessControlCardService.rebind(o);
        return R.data(Boolean.TRUE);
    }

    @PostMapping("/lossReport")
    @ApiOperation(value = "挂失门卡")
    public R lossReport(@Valid @RequestBody IdRequest o) {
        doorAccessControlCardService.lossReport(o.getId());
        return R.data(Boolean.TRUE);
    }

    @PostMapping("/cancelLossReport")
    @ApiOperation(value = "取消挂失门卡")
    public R cancelLossReport(@Valid @RequestBody IdRequest o) {
        doorAccessControlCardService.cancelLossReport(o.getId());
        return R.data(Boolean.TRUE);
    }

    @PostMapping("/bindFacial")
    @ApiOperation(value = "门卡绑定人脸")
    public R bindFacial(@Valid @RequestBody DoorAccessControlCardFacialBind o) {
        doorAccessControlCardService.bindFacial(o);
        return R.data(Boolean.TRUE);
    }

    @PostMapping("/page")
    @ApiOperation(value = "门卡分页查询")
    public R page(@Valid @RequestBody PageWrapper<DoorAccessControlCardUserCondition> o) {
        if (ObjectUtils.isEmpty(o.getT().getDepartmentIds())) {
            o.getT().setDepartmentIds(OrganizationIdExtractor.extractSet());
        }

        return R.data(doorAccessControlCardService.page(o));
    }

    @PostMapping("/pageForUser")
    @ApiOperation(value = "门卡条件查询")
    public R pageForUser(@Valid @RequestBody PageWrapper<DoorAccessControlCardConditionDTO> o) {
        if (ObjectUtils.isEmpty(o.getT().getDepartmentId())) {
            o.getT().setDepartmentId(OrganizationIdExtractor.extract());
        }

        return R.data(doorAccessControlCardService.pageForUser(o.map(DoorAccessControlCardConditionSO::from)));
    }

    @PostMapping("/stateAggregation")
    @ApiOperation(value = "门卡状态")
    public R stateAggregation(@Valid @RequestBody DoorAccessControlCardStateAggregationCondition o) {
        return R.data(doorAccessControlCardService.stateAggregation(o));
    }
}
