package com.hzs.activity.tree.controller.manage;/**
 * @Description:
 * @Author: yuhui
 * @Time: 2023/6/9 16:50
 * @Classname: TreeRuleConfigController
 * @PackageName: com.hzs.activity.tree.controller.manage
 */

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.activity.base.param.ActivityParam;
import com.hzs.activity.base.param.QueryActivityParam;
import com.hzs.activity.base.service.IAcBaseConfigService;
import com.hzs.activity.base.service.IAcGoalMemberConfigService;
import com.hzs.activity.base.service.IActivityService;
import com.hzs.activity.base.vo.BaseConfigVo;
import com.hzs.activity.tree.param.TreeOrderParam;
import com.hzs.activity.tree.service.IAcTreeRuleConfigService;
import com.hzs.activity.tree.service.ISaTreeOrderService;
import com.hzs.common.core.constant.msg.ActivityMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EActType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
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.activity.base.AcBaseConfig;
import com.hzs.common.domain.activity.base.AcGoalMemberConfig;
import com.hzs.common.domain.activity.ext.SaTreeOrderExt;
import com.hzs.common.domain.activity.tree.AcTreeRuleConfig;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.security.utils.SecurityUtils;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *@BelongsProject: hzs_cloud
 *@BelongsPackage: com.hzs.activity.tree.controller.manage
 *@Author: yh
 *@CreateTime: 2023-06-09  16:50
 *@Description: TODO
 *@Version: 1.0
 */
@RequestMapping("/manage/tree-rule-pick")
@RestController
public class TreeRuleConfigController extends BaseController {
    @DubboReference
    private IGradeServiceApi gradeServiceApi;
    @DubboReference
    private IAwardsServiceApi awardsServiceApi;
    @Autowired
    private IAcTreeRuleConfigService  treeRuleConfigService;
    @Autowired
    private IAcBaseConfigService baseConfigService;
    @Autowired
    private ISaTreeOrderService treeOrderService;
    @Autowired
    private IAcGoalMemberConfigService goalMemberConfigService;
    @Autowired
    private IActivityService IActivityService;
    /**
     * 植树活动配置添加
     * @param activityParam
     * @return
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody ActivityParam activityParam){
        if (activityParam.getTreeRuleConfigList().size() > 0){
            for (AcTreeRuleConfig treeRuleConfig : activityParam.getTreeRuleConfigList()) {
                if (StringUtils.isBlank(treeRuleConfig.getCover()) || StringUtils.isBlank(treeRuleConfig.getName())
                 || treeRuleConfig.getPkAwards() == null || treeRuleConfig.getPrice() == null || treeRuleConfig.getQuantity() == null
                ){
                    return AjaxResult.error(ActivityMsgConstants.DRAW_RULE_COMPLETE_INFO);
                }
            }
        }
        activityParam.setPkCountry(SecurityUtils.getPkCountry());
        activityParam.setPkCreator(SecurityUtils.getUserId());
        activityParam.setActType(EActType.TREE.getValue());
        treeRuleConfigService.saveTreeActivity(activityParam);
        return AjaxResult.success();
    }

    /**
     * 植树活动配置修改
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody ActivityParam activityParam){
        if (activityParam.getTreeRuleConfigList().size() > 0){
            for (AcTreeRuleConfig treeRuleConfig : activityParam.getTreeRuleConfigList()) {
                if (StringUtils.isBlank(treeRuleConfig.getCover()) || StringUtils.isBlank(treeRuleConfig.getName())
                        || treeRuleConfig.getPkAwards() == null || treeRuleConfig.getPrice() == null || treeRuleConfig.getQuantity() == null
                ){
                    return AjaxResult.error(ActivityMsgConstants.DRAW_RULE_COMPLETE_INFO);
                }
            }
        }
        activityParam.setPkCreator(SecurityUtils.getUserId());
        activityParam.setPkCountry(SecurityUtils.getPkCountry());
        treeRuleConfigService.updateTreeActivity(activityParam);
        return AjaxResult.success();
    }

    /**
     * 查询详情
     * @param pkId
     * @return
     */
    @GetMapping("get-detail")
    public AjaxResult getDetail(Integer pkId){
        AcBaseConfig baseConfig = baseConfigService.getById(pkId);
        ActivityParam activityParam = BeanUtil.copyProperties(baseConfig, ActivityParam.class);
        QueryWrapper<AcGoalMemberConfig> qe = new QueryWrapper<>();
        qe.eq("PK_BASE_ID",pkId);
        List<AcGoalMemberConfig> goalMemberConfigList = goalMemberConfigService.list(qe);
        activityParam.setGoalMemberConfigList(goalMemberConfigList);

        QueryWrapper<AcTreeRuleConfig> qw = new QueryWrapper<>();
        qw.eq("PK_BASE_ID",pkId);
        List<AcTreeRuleConfig> treeRuleConfigList = treeRuleConfigService.list(qw);
        activityParam.setTreeRuleConfigList(treeRuleConfigList);
        return AjaxResult.success(activityParam);
    }

    /**
     * 删除
     * @param pkId
     * @return
     */
    @DeleteMapping("/{pkId}")
    public AjaxResult remove(@PathVariable Long pkId) {

        treeRuleConfigService.removeTreeActivity(pkId);
        return AjaxResult.success();
    }

//    @GetMapping("list")
//    public TableDataInfo list(TreeRuleConfigParam treeRuleConfigParam){
//
//        treeRuleConfigService.queryList(treeRuleConfigParam);
//        return getDataTable()
//    }

    /**
     * 查询种植公益列表
     * @param treeOrderParam
     * @return
     */
    @GetMapping("member-info-list")
    public TableDataInfo list(TreeOrderParam treeOrderParam){

        // 奖衔
        R<List<BdAwards>> awardsDto = awardsServiceApi.queryAwards(SecurityUtils.getPkCountry());
        // 等级
        R<List<GradeDTO>> gradeDTO = gradeServiceApi.queryGradeList(SecurityUtils.getPkCountry());
        if (treeOrderParam.getStartPayDate() != null){
            treeOrderParam.setStartPayDate(DateUtils.getStartTime(treeOrderParam.getStartPayDate()));
        }
        if (treeOrderParam.getEndPayDate() != null){
            treeOrderParam.setEndPayDate(DateUtils.getEndTime(treeOrderParam.getEndPayDate()));
        }
        startPage();
        List<SaTreeOrderExt> treeOrderList = treeOrderService.selectByList(treeOrderParam);
        for (SaTreeOrderExt saTreeOrder : treeOrderList) {
            // 奖衔
            for (BdAwards awards : awardsDto.getData()) {
                if (saTreeOrder.getPkAwards().equals(awards.getPkId())){
                    saTreeOrder.setAwardsStr(awards.getAwardsName());
                }
            }
            // 等级
            for (GradeDTO grade : gradeDTO.getData()) {
                if (saTreeOrder.getPkSettleGrade().equals(grade.getPkId())){
                    saTreeOrder.setSettleGradeStr(grade.getGradeName());
                }
            }
        }
        return getDataTable(treeOrderList);
    }



    /**
     * 种植公益导出
     */
    @PostMapping("export")
    public void export (QueryActivityParam queryActivityParam, HttpServletResponse response){
        List<BaseConfigVo> resultList = IActivityService.activityList(queryActivityParam);
        ExcelUtil<BaseConfigVo> util = new ExcelUtil<>(BaseConfigVo.class);
        util.exportExcel(response, resultList, "种植公益导出");
    }


    /**
     * 种植公益明细
     * @param treeOrderParam
     * @return
     */
    @PostMapping("member-info-list-export")
    public void memberInfoListExport(TreeOrderParam treeOrderParam, HttpServletResponse response){

        // 奖衔
        R<List<BdAwards>> awardsDto = awardsServiceApi.queryAwards(SecurityUtils.getPkCountry());
        // 等级
        R<List<GradeDTO>> gradeDTO = gradeServiceApi.queryGradeList(SecurityUtils.getPkCountry());
        if (treeOrderParam.getStartPayDate() != null){
            treeOrderParam.setStartPayDate(DateUtils.getStartTime(treeOrderParam.getStartPayDate()));
        }
        if (treeOrderParam.getEndPayDate() != null){
            treeOrderParam.setEndPayDate(DateUtils.getEndTime(treeOrderParam.getEndPayDate()));
        }
        List<SaTreeOrderExt> treeOrderList = treeOrderService.selectByList(treeOrderParam);
        for (SaTreeOrderExt saTreeOrder : treeOrderList) {
            // 奖衔
            for (BdAwards awards : awardsDto.getData()) {
                if (saTreeOrder.getPkAwards().equals(awards.getPkId())){
                    saTreeOrder.setAwardsStr(awards.getAwardsName());
                }
            }
            // 等级
            for (GradeDTO grade : gradeDTO.getData()) {
                if (saTreeOrder.getPkSettleGrade().equals(grade.getPkId())){
                    saTreeOrder.setSettleGradeStr(grade.getGradeName());
                }
            }
        }
        ExcelUtil<SaTreeOrderExt> util = new ExcelUtil<>(SaTreeOrderExt.class);
        util.exportExcel(response, treeOrderList, "种植公益明细导出");
    }
}
