package com.hzs.member.base.controller.manager;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.HonorWallType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.honor.CuHonorWall;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.param.HonorWallParam;
import com.hzs.member.base.service.ICuHonorWallService;
import com.hzs.member.base.vo.CuHonorWallVo;
import com.hzs.system.config.IAwardsServiceApi;
import com.hzs.system.config.IGradeServiceApi;
import com.hzs.system.config.dto.GradeDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/manage/member")
@RestController
public class CuHonorWallController extends BaseController {

    @Autowired
    private ICuHonorWallService honorWallService;

    @DubboReference
    IGradeServiceApi gradeServiceApi;
    @DubboReference
    IAwardsServiceApi awardsServiceApi;

    /**
     * 查询荣誉墙列表
     *
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(HonorWallParam honorWallParam) {
        startPage();
        R<List<GradeDTO>> gradeDto = gradeServiceApi.queryGradeList(SecurityUtils.getPkCountry());
        R<List<BdAwards>> awardsDto = awardsServiceApi.queryAwards(SecurityUtils.getPkCountry());
        QueryWrapper<CuHonorWall> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if (StringUtils.isNotBlank(honorWallParam.getName())) {
            queryWrapper.eq("NAME", honorWallParam.getName());
        }
        if (honorWallParam.getStartCreateTime() != null && honorWallParam.getEndCreateTime() != null) {
            queryWrapper.between("CREATION_TIME", honorWallParam.getStartCreateTime(), honorWallParam.getEndCreateTime());
        }
        if (honorWallParam.getType() != null) {
            queryWrapper.eq("TYPE", honorWallParam.getType());
        }
        queryWrapper.orderByAsc("type", "value");
        List<CuHonorWall> list = honorWallService.list(queryWrapper);
        List<CuHonorWallVo> resultList = new ArrayList<>();
        for (CuHonorWall cuHonorWall : list) {
            CuHonorWallVo cuHonorWallExt = BeanUtil.copyProperties(cuHonorWall, CuHonorWallVo.class);
            cuHonorWallExt.setTypeStr(HonorWallType.getHonorWallType(cuHonorWall.getType()).getLabel());
            cuHonorWallExt.setName(cuHonorWall.getName());
            cuHonorWallExt.setTitle(cuHonorWall.getTitle());
            // 检测 属性值转换
            // 等级
            if (cuHonorWall.getType().equals(HonorWallType.GRADE.getValue())) {
                for (GradeDTO date : gradeDto.getData()) {
                    if (date.getPkId().equals(cuHonorWall.getValue())) {
                        cuHonorWallExt.setValueStr(date.getGradeName());
                    }
                }
                // 奖衔
            } else if (cuHonorWall.getType().equals(HonorWallType.AWARDS.getValue())) {
                for (BdAwards date : awardsDto.getData()) {
                    if (date.getPkId().equals(cuHonorWall.getValue())) {
                        cuHonorWallExt.setValueStr(date.getAwardsName());
                    }
                }
            } else {
                if (cuHonorWall.getType().equals(HonorWallType.DURATION.getValue())) {
                    cuHonorWallExt.setValueStr(cuHonorWallExt.getValue() + "小时");
                } else if (cuHonorWall.getType().equals(HonorWallType.RECOMMEND_MONTH.getValue())) {
                    cuHonorWallExt.setValueStr(cuHonorWallExt.getValue() + "月");
                } else if (cuHonorWall.getType().equals(HonorWallType.RECOMMEND_QUARTER.getValue())) {
                    cuHonorWallExt.setValueStr(cuHonorWallExt.getValue() + "季度");
                } else if (cuHonorWall.getType().equals(HonorWallType.RECOMMEND_YEAR.getValue())) {
                    cuHonorWallExt.setValueStr(cuHonorWallExt.getValue() + "年");
                }

            }
            resultList.add(cuHonorWallExt);
        }
        TableDataInfo tableDataInfo = getDataTable(list);
        tableDataInfo.setRows(resultList);
        return tableDataInfo;
    }


    /**
     * 新增荣誉墙
     *
     * @param honorWall
     * @return
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody CuHonorWall honorWall) {
        honorWall.setPkCountry(SecurityUtils.getPkCountry());
        honorWall.setCreationTime(DateUtils.currentDateTime());
        honorWall.setPkCreator(SecurityUtils.getUserId());

        honorWallService.save(honorWall);
        return AjaxResult.success();
    }

    /**
     * 修改荣誉墙
     *
     * @param honorWall
     * @return
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody CuHonorWall honorWall) {
        honorWall.setModifiedTime(DateUtils.currentDateTime());
        honorWall.setPkModified(SecurityUtils.getUserId());
        honorWallService.updateById(honorWall);
        return AjaxResult.success();
    }

    /**
     * 删除荣誉墙
     *
     * @param pkId
     * @return
     */
    @DeleteMapping("/{pkId}")
    public AjaxResult remove(@PathVariable Integer pkId) {
        honorWallService.removeById(pkId);
        return AjaxResult.success();
    }

}
