package com.gree.mall.manager.logic.admin;

import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gree.mall.manager.bean.admin.reqDto.AdminCompanyWechatTemplateUpdateReqDto;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.plus.entity.AdminCompanyWechat;
import com.gree.mall.manager.plus.entity.AdminCompanyWechatTemplate;
import com.gree.mall.manager.plus.service.AdminCompanyWechatTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class AdminCompanyWechatTemplateLogic {


    @Autowired
    private AdminCompanyWechatTemplateService adminCompanyWechatTemplateService;

    /**
     * 获取企业下模板
     */
    public List<AdminCompanyWechatTemplate> getTemplate(String companyId) {
        return adminCompanyWechatTemplateService.lambdaQuery()
                .eq(AdminCompanyWechatTemplate::getCompanyWechatId, companyId)
                .list();
    }


    /**
     * 更改模板信息
     */
    public void updateTemplate(HttpServletRequest request, AdminCompanyWechatTemplate adminCompanyWechatTemplate) {

        if (StringUtils.isBlank(adminCompanyWechatTemplate.getCompanyWechatTemplateId())) {
            throw new RemoteServiceException("模板id不能为null");
        }
        adminCompanyWechatTemplate.setTemplateType(null);
        adminCompanyWechatTemplateService.updateById(adminCompanyWechatTemplate);

    }

    /**
     * 更改企业信息和模板信息
     */
    public void updateCompanyWithTemplate(AdminCompanyWechatTemplateUpdateReqDto reqDto){

        if(StringUtils.isBlank(reqDto.getCompanyWechatId())){
            throw new RemoteServiceException("企业微信id不能为null");
        }

        AdminCompanyWechat adminCompanyWechat = new AdminCompanyWechat();
        adminCompanyWechat.setCompanyWechatId(reqDto.getCompanyWechatId());
        adminCompanyWechat.setUseTemplate(reqDto.getUseTemplate());
        adminCompanyWechat.setMinLogo1(reqDto.getMainLog());
        adminCompanyWechat.setMinLogo2(reqDto.getCommonLog());
        adminCompanyWechat.setMinLogo3(reqDto.getMinLogo());
        adminCompanyWechat.updateById();

        List<AdminCompanyWechatTemplate> templateList = reqDto.getCompanyWechatTemplatesList();
        if(CollectionUtils.isNotEmpty(templateList)){
            for (AdminCompanyWechatTemplate template:templateList
                 ) {
                if(StringUtils.isBlank(template.getCompanyWechatTemplateId())){
                    throw new RemoteServiceException("小程序模板id不能为null");
                }
                template.setTemplateType(null);
            }
            adminCompanyWechatTemplateService.updateBatchById(templateList);
        }

    }


    /**
     * 添加商户时 初始化企业微信模板 添加新模板后需要手动跟新
     */
    public void initCompanyWechatTemplate(String companyWechatId, String companyName) {

        //清除旧模板
        adminCompanyWechatTemplateService.lambdaUpdate()
                .eq(AdminCompanyWechatTemplate::getCompanyWechatId, companyWechatId)
                .remove();

        List<AdminCompanyWechatTemplate> templateList = new ArrayList<>();

        for (int i = 1; i <= 2; i++) {

            AdminCompanyWechatTemplate data = new AdminCompanyWechatTemplate();
            templateList.add(data);

            data.setCompanyWechatId(companyWechatId);
            data.setCompanyName(companyName);
            data.setTemplateType(i);

            if(i==1){
                data.setCategoryStatus(true);
                data.setCarouseStatus(true);
                data.setKillStatus(true);
                data.setOkStatus(true);
            }
            if(i==2){
                data.setPopupStatus(true);
                data.setCarouseStatus(true);
                data.setActive1Status(true);
                data.setCategoryStatus(true);
                data.setKillStatus(true);
                data.setCouponStatus(true);
                data.setNewsStatus(true);
            }

        }

        adminCompanyWechatTemplateService.saveBatch(templateList);

    }

}
