package com.fjhx.victoriatourist.controller.blessing.open;

import com.fjhx.tenant.entity.dict.vo.DictTenantDataVo;
import com.fjhx.victoriatourist.entity.blessing.dto.BlessingCoverSelectDto;
import com.fjhx.victoriatourist.entity.blessing.dto.BlessingInfoDto;
import com.fjhx.victoriatourist.entity.blessing.dto.BlessingInfoSelectDto;
import com.fjhx.victoriatourist.entity.blessing.dto.BlessingLanguageSelectDto;
import com.fjhx.victoriatourist.entity.blessing.vo.BlessingCoverVo;
import com.fjhx.victoriatourist.entity.blessing.vo.BlessingInfoVo;
import com.fjhx.victoriatourist.entity.blessing.vo.BlessingLanguageVo;
import com.fjhx.victoriatourist.entity.blessing.vo.open.BlessingGradeVo;
import com.fjhx.victoriatourist.service.blessing.BlessingCoverService;
import com.fjhx.victoriatourist.service.blessing.BlessingInfoService;
import com.fjhx.victoriatourist.service.blessing.BlessingLanguageService;
import com.fjhx.victoriatourist.service.jd.JdOrderService;
import com.fjhx.victoriatourist.service.jd.constants.RunParamConstant;
import com.fjhx.victoriatourist.utils.DictUtilsByOpen;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseSelectDto;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 祝福封面 前端控制器
 * </p>
 *
 * @author lqh
 * @since 2023-12-09
 */
@RestController
@RequestMapping("/open/blessing")
public class BlessingOpenController {

    @Autowired
    private BlessingCoverService blessingCoverService;

    @Autowired
    private BlessingLanguageService blessingLanguageService;

    @Autowired
    private BlessingInfoService blessingInfoService;

    @Autowired
    private JdOrderService jdOrderService;

    /**
     * 祝福语列表
     */
    @PostMapping("/listByLanguage")
    public List<BlessingGradeVo> listByLanguage(@RequestBody BlessingLanguageSelectDto dto) {


        Map<String, List<BlessingLanguageVo>> listMap = blessingLanguageService.getList(dto).stream().collect(Collectors.groupingBy(BlessingLanguageVo::getBlessingType));
        List<DictTenantDataVo> dictList = DictUtilsByOpen.getDictList("blessing_type");
        return dictList.stream().map(x -> {
            BlessingGradeVo vo = new BlessingGradeVo();
            vo.setDictTenantDataVo(x);

            if (listMap.containsKey(x.getDictKey())) {
                List<BlessingLanguageVo> collect = listMap.get(x.getDictKey()).stream().map(obj -> {
                    obj.setBlessingTypeName(x.getDictValue());
                    return obj;
                }).collect(Collectors.toList());
                vo.setLanguageVoList(collect);
            }else {
                vo.setLanguageVoList(new ArrayList<>());
            }
            return vo;
        }).filter(x -> !x.getLanguageVoList().isEmpty()).collect(Collectors.toList());
    }

    /**
     * 祝福封面列表
     */
    @PostMapping("/listByCover")
    public List<BlessingGradeVo> listByCover(@RequestBody BlessingCoverSelectDto dto) {
        Map<String, List<BlessingCoverVo>> listMap = blessingCoverService.getList(dto).stream().collect(Collectors.groupingBy(BlessingCoverVo::getCoverType));
        List<DictTenantDataVo> dictList = DictUtilsByOpen.getDictList("cover_type");
        return dictList.stream().map(x -> {
            BlessingGradeVo vo = new BlessingGradeVo();
            vo.setDictTenantDataVo(x);
            if (listMap.containsKey(x.getDictKey())) {
                vo.setCoverVoList(listMap.get(x.getDictKey()));
            }else {
                vo.setCoverVoList(new ArrayList<>());
            }
            return vo;
        }).filter(x -> !x.getCoverVoList().isEmpty()).collect(Collectors.toList());
    }

    /**
     * 祝福留言信息新增
     */
    @PostMapping("/saveBlessingInfo")
    public AjaxResult add(@RequestBody BlessingInfoDto blessingInfoDto) {
        SecurityUtils.setTenantId(RunParamConstant.tenantId);
        boolean b = jdOrderService.validJdOrderId(Long.valueOf(blessingInfoDto.getJdOrder()));
        SecurityUtils.clearTenantId();
        if (!b){
            return  AjaxResult.error("无此订单号");
        }
        blessingInfoService.add(blessingInfoDto);

        return  AjaxResult.success();
    }

    @PostMapping("/selectBlessingInfoList")
    public List<BlessingInfoVo> selectBlessingInfoList(@RequestBody BlessingInfoSelectDto dto) {
        return blessingInfoService.getList(dto);
    }
}
