package com.hzs.system.config.controller.manage;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.ECloudType;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
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.system.config.BdBonusCloud;
import com.hzs.common.domain.system.config.ext.BdBonusCloudExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.config.service.IBdBonusCloudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 *  云代配置
 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-11-01
 */
@RestController
@RequestMapping("/manage/bd-bonus-cloud")
public class BdBonusCloudController extends BaseController {
    @Autowired
    private IBdBonusCloudService bonusCloudService;

    /**
     * @description: 云代列表
     * @author: zhang jing
     * @date: 2022/10/31 16:32
     * @param: [bonusCloud]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BD_BONUS_CLOUD, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdBonusCloud bonusCloud) {
        QueryWrapper<BdBonusCloud> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        List<BdBonusCloud> list = bonusCloudService.list(queryWrapper);
        List<BdBonusCloudExt> listExt=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(list)){
            list.forEach(item -> listExt.add(BeanUtil.copyProperties(item, BdBonusCloudExt.class)));
        }else{
            for (ECloudType value : ECloudType.values()) {
                BdBonusCloudExt bcExt=new BdBonusCloudExt();
                bcExt.setCloudTypeVal(value.getLabel());
                bcExt.setCloudType(value.getValue());
                listExt.add(bcExt);
            }
        }
        return getDataTable(listExt);
    }



    /**
     * @description: 新增云代配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [bonusCloud]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_BONUS_CLOUD, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdBonusCloudExt bonusCloudExt) {
        if(CollectionUtil.isNotEmpty(bonusCloudExt.getBonusCloudList())){
                    for(BdBonusCloud bonusCloud:bonusCloudExt.getBonusCloudList()){
                        if(StringUtils.isNull(bonusCloud.getCloudType())){
                            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.CLOUD_TYPE_NOT_NULL));
                        }
                        if(StringUtils.isNull(bonusCloud.getCloudRatio())){
                            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.CLOUD_RATIO_NOT_NULL));
                        }
                        bonusCloud.setPkCountry(SecurityUtils.getPkCountry());
                        bonusCloud.setPkCreator(SecurityUtils.getUserId());
                        bonusCloud.setCreationTime(new Date());
                    }
            return AjaxResult.success(bonusCloudService.saveBonusCloud(bonusCloudExt));
        }else{
            return AjaxResult.error();
        }
    }




    /**
     * @description: 修改云代配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [bonusCloud]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_BONUS_CLOUD, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdBonusCloudExt bonusCloudExt) {
        if(CollectionUtil.isNotEmpty(bonusCloudExt.getBonusCloudList())){
            for(BdBonusCloud bonusCloud:bonusCloudExt.getBonusCloudList()){
                if(StringUtils.isNull(bonusCloud.getCloudType())){
                    return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.CLOUD_TYPE_NOT_NULL));
                }
                if(StringUtils.isNull(bonusCloud.getCloudRatio())){
                    return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.CLOUD_RATIO_NOT_NULL));
                }
                bonusCloud.setPkCountry(SecurityUtils.getPkCountry());
                bonusCloud.setPkCreator(SecurityUtils.getUserId());
                bonusCloud.setCreationTime(new Date());
            }
            return AjaxResult.success(bonusCloudService.updateBonusCloud(bonusCloudExt));
        }else{
            return AjaxResult.error();
        }
    }

}
