package jnpf.storecertificatephoto.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jnpf.base.ActionResult;
import jnpf.certificate.service.CertificateInstanceService;
import jnpf.model.certificate.po.CertificateInstanceEntity;
import jnpf.model.storecertificatephoto.po.StoreCertificatePhotoEntity;
import jnpf.model.storecertificatephoto.req.StoreCertificatePhotoAddReq;
import jnpf.model.storecertificatephoto.req.StoreCertificatePhotoUpdateReq;
import jnpf.model.storecertificatephoto.vo.StoreCertificatePhotoVO;
import jnpf.model.warningnotice.enums.CertificateTypeEnum;
import jnpf.permission.StoreApi;
import jnpf.permission.V2OrganizeApi;
import jnpf.permission.dto.store.StoreBaseInfoIdsQueryDTO;
import jnpf.permission.vo.store.BaseStoreVO;
import jnpf.permission.vo.v2.organzie.OrganizeGeneralDetailVO;
import jnpf.storecertificatephoto.service.StoreCertificatePhotoService;
import jnpf.util.UserProvider;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.*;
import java.util.stream.Collectors;

/**
 * web门店自定义证件配置照控制器
 */
@RestController
@Validated
@RequestMapping("/web/store-certificate-photo")
public class StoreCertificatePhotoController {

    @Autowired
    private StoreCertificatePhotoService storeCertificatePhotoService;
    @Autowired
    private CertificateInstanceService certificateInstanceService;
    @Autowired
    private V2OrganizeApi v2OrganizeApi;
    @Autowired
    private StoreApi storeApi;

    /**
     * 新增门店证件照配置
     *
     * @param req 门店证件照请求参数
     * @return 操作结果
     */
    @PostMapping("/add")
    public ActionResult<Void> add(@Validated @RequestBody StoreCertificatePhotoAddReq req) {
        storeCertificatePhotoService.add(req);
        return ActionResult.success();
    }

    /**
     * 编辑门店自定义证件配置配置
     *
     * @param req 门店证件照编辑参数
     * @return 操作结果
     */
    @PutMapping("/update")
    public ActionResult<Void> update(@Validated @RequestBody StoreCertificatePhotoUpdateReq req) {
        storeCertificatePhotoService.update(req);
        return ActionResult.success();
    }

    /**
     * 删除门店自定义证件配置配置
     *
     * @param id 门店证照自定义配置id
     * @return 操作结果
     */
    @DeleteMapping("/delete/{id}")
    public ActionResult<Void> delete(@PathVariable("id") String id) {
        Set<String> orgIds = certificateInstanceService.list(new LambdaQueryWrapper<CertificateInstanceEntity>()
                .eq(CertificateInstanceEntity::getTemplateId,id)
                .eq(CertificateInstanceEntity::getEnabledMark,0))
                .stream()
                .map(CertificateInstanceEntity::getSubjectId)
                .collect(Collectors.toSet());
        if(CollectionUtils.isNotEmpty(orgIds)){
            List<BaseStoreVO> baseStoreVOS = storeApi.listBaseInfoByIds(new StoreBaseInfoIdsQueryDTO(orgIds,UserProvider.getUser().getTenantId()));
            String orgNames = baseStoreVOS.stream().map(BaseStoreVO::getStoreName).collect(Collectors.joining(","));
            return ActionResult.fail(400,"已被["+orgNames+"]门店使用,不能删除!");
        }

        boolean succ = storeCertificatePhotoService.update(new LambdaUpdateWrapper<StoreCertificatePhotoEntity>()
                .eq(StoreCertificatePhotoEntity::getId,id)
                .set(StoreCertificatePhotoEntity::getEnabledMark,1)
                .set(StoreCertificatePhotoEntity::getLastModifyUserId, UserProvider.getLoginUserId())
                .set(StoreCertificatePhotoEntity::getLastModifyTime,new Date()));
        return ActionResult.success();
    }

    /**
     * 查询门店自定义证件配置列表
     *
     * @return 门店证件照列表
     */
    @GetMapping("/query-list")
    public ActionResult<List<StoreCertificatePhotoVO>> queryList() {
        return ActionResult.success(storeCertificatePhotoService.queryList());
    }

    /**
     * 根据ID查询门店自定义证件配置照详情
     *
     * @param id 主键ID
     * @return 门店证件照详情
     */
    @GetMapping("/query-info/{id}")
    public ActionResult<StoreCertificatePhotoVO> queryInfo(@PathVariable("id") @NotBlank(message = "主键ID不能为空") String id) {
        return ActionResult.success(storeCertificatePhotoService.queryInfo(id));
    }
}
