package com.hzs.activity.work.controller.manage;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.activity.people.vo.AcBaseConfigVO;
import com.hzs.activity.work.service.IAcSuperWorkRuleConfigService;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.*;
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.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.sys.dto.LoginUser;
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;

/**
 * <p>
 * 超级工作室赠送 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2023-04-21
 */
@RestController
@RequestMapping("/manage/supe-rwork")
public class AcSuperWorkRuleConfigController extends BaseController {
    @Autowired
    private IAcSuperWorkRuleConfigService iAcSupeRworkRuleConfigService;
    @Autowired
    private UserTokenService userTokenService;


    /**
     * @description: 添加超级工作室赠送-发起签呈
     * @author: zhang jing
     * @date: 2023/4/21 14:48
     * @param: [supVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AC_SUPER_WORK_RULE_CONFIG, method = EOperationMethod.INSERT)
    @PostMapping("/save-petition")
    public AjaxResult savePetition(@RequestBody AcBaseConfigVO supVO) {
        // 验证参数
        supVO.setControlType(EControlType.SAVE.getValue());
        List<String> errorList = validateParams(supVO);
        if (!errorList.isEmpty()) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_ERROR), errorList);
        }
        // 设置参数
        LoginUser loginUser = userTokenService.getLoginUser();
        supVO.setLoginUser(loginUser);
        supVO.setPkCountry(SecurityUtils.getPkCountry());
        supVO.setActType(EActType.SUPER_STUDIO_GIFT.getValue());
        return AjaxResult.success(iAcSupeRworkRuleConfigService.petition(supVO));
    }


    /**
     * @description: 修改超级工作室赠送-发起签呈
     * @author: zhang jing
     * @date: 2023/4/22 11:12
     * @param: [supVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AC_SUPER_WORK_RULE_CONFIG, method = EOperationMethod.UPDATE)
    @PostMapping("/up-petition")
    public AjaxResult upPetition(@RequestBody AcBaseConfigVO supVO) {
        // 验证参数
        supVO.setControlType(EControlType.MODIFY.getValue());
        List<String> errorList = validateParams(supVO);
        if (!errorList.isEmpty()) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_ERROR, errorList));
        }
        // 设置参数
        LoginUser loginUser = userTokenService.getLoginUser();
        supVO.setLoginUser(loginUser);
        supVO.setPkCountry(SecurityUtils.getPkCountry());
        supVO.setActType(EActType.SUPER_STUDIO_GIFT.getValue());
        return AjaxResult.success(iAcSupeRworkRuleConfigService.petition(supVO));
    }

    /**
     * @description: 删除超级工作室赠送-发起签呈
     * @author: zhang jing
     * @date: 2023/4/25 17:49
     * @param: [supVO]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AC_SUPER_WORK_RULE_CONFIG, method = EOperationMethod.DELETE)
    @PostMapping("/del-petition")
    public AjaxResult delPetition(@RequestBody AcBaseConfigVO supVO) {
        supVO.setControlType(EControlType.DELETE.getValue());
        List<String> errorList = validateParams(supVO);
        if (!errorList.isEmpty()) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_ERROR), errorList);
        }
        return AjaxResult.success(iAcSupeRworkRuleConfigService.petition(supVO));
    }

    // 验证参数
    private List<String> validateParams(AcBaseConfigVO anpVO) {
        List<String> errorList = new ArrayList<>();
        if (StringUtils.isNull(anpVO)) {
            errorList.add(TransactionUtils.getContent(ConfigMsgConstants.ACTIVITY_NOT_NULL));
        }
        if (CollectionUtil.isNotEmpty(anpVO.getAswList())) {
            errorList.add(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_DATA_NOT_NULL));
        }
        if (StringUtils.isNull(anpVO.getSignType())) {
            errorList.add(TransactionUtils.getContent(ConfigMsgConstants.SIGN_TYPE_NOT_NULL));
        }
        if (StringUtils.isNotNull(anpVO.getActName())) {
            errorList.add(TransactionUtils.getContent(ConfigMsgConstants.ACTIVITY_NAME_NOT_NULL));
        }
        if (StringUtils.isNotNull(anpVO.getActExplain())) {
            errorList.add(TransactionUtils.getContent(ConfigMsgConstants.ACTEXPLAIN_NOT_NULL));
        }
        if (StringUtils.isNotNull(anpVO.getActStartDate())) {
            errorList.add(TransactionUtils.getContent(ConfigMsgConstants.ACTSTARTDATE_NOT_NULL));
        }
        if (StringUtils.isNotNull(anpVO.getActEndDate())) {
            errorList.add(TransactionUtils.getContent(ConfigMsgConstants.ACTENDDATE_NOT_NULL));
        }
        if (StringUtils.isNotNull(anpVO.getDisStartDate())) {
            errorList.add(TransactionUtils.getContent(ConfigMsgConstants.DISSTARTDATE_NOT_NULL));
        }
        if (StringUtils.isNotNull(anpVO.getDisEndDate())) {
            errorList.add(TransactionUtils.getContent(ConfigMsgConstants.DISENDDATE_NOT_NULL));
        }
        return errorList;
    }

}
