package com.crm.manager.controller;

import com.crm.manager.service.shop.ShopSysIntegralRuleFeignService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.system.shop.integral.SysIntegralRuleAllDto;
import com.crm.rely.backend.core.entity.system.shop.integral.SysIntegralRuleUpdateAllEntity;
import com.crm.rely.backend.core.entity.system.shop.integral.SysIntegralRuleUpdateEntity;
import com.crm.rely.backend.exception.ControllerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
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.List;

/**
 * Created by max on 2021/3/24.
 */
@RestController
@RequestMapping("/shop/sys/integral/rule")
public class ShopSysIntegralRuleController {

    @Autowired
    private ShopSysIntegralRuleFeignService shopSysIntegralRuleFeignService;

    @PostMapping("/update")
    public BaseResultDto<Object> updateForm(@RequestBody @Validated SysIntegralRuleUpdateAllEntity entity) throws Exception {

        if (!validatePoint(entity.getOther())) {
            throw new ControllerException("parameter_error");
        }

        if (!validatePoint(entity.getIndex())) {
            throw new ControllerException("parameter_error");
        }
        //更新操作信息
        setOperateInfo(entity);

        return shopSysIntegralRuleFeignService.updateForm(entity);
    }

    @PostMapping("/search/all")
    public BaseResultDto<SysIntegralRuleAllDto> searchAll() throws Exception{
        return shopSysIntegralRuleFeignService.searchAll();
    }

    private boolean validatePoint(List<SysIntegralRuleUpdateEntity> entities) throws Exception {
        if (CollectionUtils.isEmpty(entities)){
            return true;
        }
        boolean result = true;
        for (SysIntegralRuleUpdateEntity sysIntegralRuleUpdateEntity : entities) {
            if (sysIntegralRuleUpdateEntity.getTotal() < sysIntegralRuleUpdateEntity.getCustom() + sysIntegralRuleUpdateEntity.getLevelOne() + sysIntegralRuleUpdateEntity.getLevelTwo() + sysIntegralRuleUpdateEntity.getLevelThree()) {
                result = false;
                break;
            }
        }
        return result;
    }

    private void setOperateInfo(SysIntegralRuleUpdateAllEntity entity) throws Exception{

        if (!CollectionUtils.isEmpty(entity.getOther())){
            for (SysIntegralRuleUpdateEntity sysIntegralRuleUpdateEntity : entity.getOther()) {
                sysIntegralRuleUpdateEntity.setModifyTime(entity.getModifyTime());
                sysIntegralRuleUpdateEntity.setModifyIp(entity.getModifyIp());
                sysIntegralRuleUpdateEntity.setModifyUser(entity.getModifyUser());
            }
        }

        if (!CollectionUtils.isEmpty(entity.getIndex())){
            for (SysIntegralRuleUpdateEntity sysIntegralRuleUpdateEntity : entity.getIndex()) {
                sysIntegralRuleUpdateEntity.setModifyTime(entity.getModifyTime());
                sysIntegralRuleUpdateEntity.setModifyIp(entity.getModifyIp());
                sysIntegralRuleUpdateEntity.setModifyUser(entity.getModifyUser());
            }
        }

    }

}
