package com.gic.haoban.manage.web.controller.hm;

import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.manage.api.dto.hm.HmPageDTO;
import com.gic.haoban.manage.api.dto.hm.HmPageRelationDTO;
import com.gic.haoban.manage.api.dto.qdto.hm.HmPageQDTO;
import com.gic.haoban.manage.api.dto.qdto.hm.HmPageRelationQDTO;
import com.gic.haoban.manage.api.service.hm.HmPageApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.manage.web.log.LogRecordUserServiceImpl;
import com.gic.haoban.manage.web.qo.hm.HmPageListQO;
import com.gic.haoban.manage.web.qo.hm.HmPageSettingReq;
import com.gic.haoban.manage.web.vo.hm.PageRelationVO;
import com.gic.log.record.anno.GicLogRecord;
import com.gic.log.record.util.GicLogRecordCategoryEnum;
import com.gic.log.record.util.GicLogRecordEvaluationContext;
import com.gic.log.record.util.GicLogRecordOptTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @Author MUSI
 * @Date 2022/7/4 1:58 PM
 * @Description
 * @Version
 **/
@RestController
@RequestMapping(path = "/hm/guide")
public class HmPageController {

    private static final Logger log = LoggerFactory.getLogger(HmPageController.class);
    @Autowired
    private HmPageApiService pageApiService;

    @GicLogRecord(value = "${#logContent}", category = GicLogRecordCategoryEnum.HUOMA, optType = GicLogRecordOptTypeEnum.HUOMA_PAGE_ADD
    ,optPage = "引流链接-落地页模板-新建模板", userFunc = LogRecordUserServiceImpl.class )
    @RequestMapping(path = "/page/save")
    public RestResponse<?> savePage(@Valid HmPageSettingReq pageSettingReq){
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        HmPageDTO pageDTO = new HmPageDTO();
        pageDTO.setPageName(pageSettingReq.getPageName());
        pageDTO.setPageTitle(pageSettingReq.getPageTitle());
        pageDTO.setMerchantName(pageSettingReq.getMerchantName());
        pageDTO.setMerchantPhoneNumber(pageSettingReq.getMerchantPhoneNumber());
        pageDTO.setMerchantLogo(pageSettingReq.getMerchantLogo());
        pageDTO.setPageBackgroundImg(pageSettingReq.getPageBackgroundImg());
        pageDTO.setGuideComment(pageSettingReq.getGuideComment());
        pageDTO.setCreateorId(loginUser.getClerkId());
        pageDTO.setCreateorName(loginUser.getClerkName());
        pageDTO.setEnterpriseId(loginUser.getEnterpriseId());
        pageDTO.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        pageDTO.setPageType(pageSettingReq.getPageType());
        ServiceResponse<Long> serviceResponse = pageApiService.saveOrUpdateHmPage(pageDTO);
        if (serviceResponse.isSuccess()){
            String logContent = "新增落地页模板【MB01" + serviceResponse.getResult()+"-" + pageSettingReq.getPageName() +"】";
            GicLogRecordEvaluationContext.putAttribute("logContent", logContent);
            return RestResponse.successResult(serviceResponse.getResult());
        }
        GicLogRecordEvaluationContext.noWriteLog();
        return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
    }


    @GicLogRecord(value = "${#logContent}", category = GicLogRecordCategoryEnum.HUOMA, optType = GicLogRecordOptTypeEnum.HUOMA_PAGE_UPDATE
            ,optPage = "引流链接-落地页模板-编辑模板", userFunc = LogRecordUserServiceImpl.class )
    @RequestMapping(path = "/page/edit")
    public RestResponse<?> editPage(@Valid HmPageSettingReq pageSettingReq){

        if (pageSettingReq.getPageId() == null){
            log.warn("[editPage] 无pageId");
            return RestResponse.failure(HaoBanErrCode.ERR_2.getCode()+"", HaoBanErrCode.ERR_2.getMsg());
        }

        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        HmPageDTO pageDTO = new HmPageDTO();
        pageDTO.setPageId(pageSettingReq.getPageId());
        pageDTO.setPageName(pageSettingReq.getPageName());
        pageDTO.setPageTitle(pageSettingReq.getPageTitle());
        pageDTO.setMerchantName(pageSettingReq.getMerchantName());
        pageDTO.setMerchantPhoneNumber(pageSettingReq.getMerchantPhoneNumber());
        pageDTO.setMerchantLogo(pageSettingReq.getMerchantLogo());
        pageDTO.setPageBackgroundImg(pageSettingReq.getPageBackgroundImg());
        pageDTO.setGuideComment(pageSettingReq.getGuideComment());
        pageDTO.setCreateorId(loginUser.getClerkId());
        pageDTO.setCreateorName(loginUser.getClerkName());
        pageDTO.setEnterpriseId(loginUser.getEnterpriseId());
        pageDTO.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        ServiceResponse<HmPageDTO> queryPageDetail = pageApiService.queryPageDetail(pageDTO.getPageId());
        ServiceResponse<Long> serviceResponse = pageApiService.saveOrUpdateHmPage(pageDTO);
        if (serviceResponse.isSuccess()){
            String logContent = this.buildEditPageContent(pageDTO, queryPageDetail.getResult());
            GicLogRecordEvaluationContext.putAttribute("logContent", logContent);
            return RestResponse.successResult(serviceResponse.getResult());
        }
        GicLogRecordEvaluationContext.noWriteLog();
        return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
    }

    /**
     * 编辑落地页模板【模板ID-模板名称】：模板名称「 模板旧名称」变更「 模板新名称」，
     * 页面标题「 页面旧标题」便跟「 页面新标题」，
     * 商户名称「 商户名称」变更「 商户名称」，
     * 商户电话「 商户电话」变更「 商户电话」，
     * 商户LOGO、页面背景、
     * 引导语「 引导语」变更「 引导语」
     * @param newPageDTO
     * @param oldPageDTO
     * @return
     */
    private String buildEditPageContent(HmPageDTO newPageDTO, HmPageDTO oldPageDTO){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("编辑落地页模板【")
                .append(oldPageDTO.getPageCode())
                .append("-")
                .append(oldPageDTO.getPageName())
                .append("：模板名称「 ")
                .append(oldPageDTO.getPageName())
                .append("」变更为「 ")
                .append(newPageDTO.getPageName())
                .append("」，页面标题「 ")
                .append(oldPageDTO.getPageTitle())
                .append("」变更为「")
                .append(newPageDTO.getPageTitle())
                .append("」，商户名称「")
                .append(oldPageDTO.getMerchantName())
                .append("」变更为「 ")
                .append(newPageDTO.getMerchantName())
                .append("」，商户电话「 ")
                .append(oldPageDTO.getMerchantPhoneNumber())
                .append("」变更为「")
                .append(newPageDTO.getMerchantPhoneNumber())
                .append("」，引导语「 ")
                .append(oldPageDTO.getGuideComment())
                .append("」变更为「")
                .append(newPageDTO.getGuideComment())
                .append("」");
        return stringBuilder.toString();
    }

    @RequestMapping(path = "/page/list")
    public RestResponse<?> queryPageList(HmPageListQO pageListVO){

        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        HmPageQDTO hmPageQDTO = new HmPageQDTO();
        hmPageQDTO.setPageNum(pageListVO.getPageNum());
        hmPageQDTO.setPageSize(pageListVO.getPageSize());
        hmPageQDTO.setEnterpriseId(loginUser.getEnterpriseId());
        hmPageQDTO.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        hmPageQDTO.setPageSearchText(pageListVO.getPageSearchText());
        hmPageQDTO.setStartTime(pageListVO.getStartTime());
        hmPageQDTO.setEndTime(pageListVO.getEndTime());
        hmPageQDTO.setPageType(pageListVO.getPageType());
        ServiceResponse<Page<HmPageDTO>> serviceResponse = pageApiService.queryPageList(hmPageQDTO);
        return RestResponse.successResult(serviceResponse.getResult());
    }

    @GicLogRecord(value = "${#logContent}", category = GicLogRecordCategoryEnum.HUOMA, optType = GicLogRecordOptTypeEnum.HUOMA_PAGE_DEL
            ,optPage = "引流链接-落地页模板", userFunc = LogRecordUserServiceImpl.class )
    @RequestMapping(path = "/page/delete")
    public RestResponse<?> deletePage(Long pageId){
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        HmPageDTO hmPageDTO = new HmPageDTO();
        hmPageDTO.setPageId(pageId);
        hmPageDTO.setModifierId(loginUser.getClerkId());
        hmPageDTO.setModifierName(loginUser.getClerkName());

        ServiceResponse<HmPageDTO> queryPageDetail = pageApiService.queryPageDetail(pageId);
        ServiceResponse<Boolean> serviceResponse = pageApiService.deleteHmPage(hmPageDTO);
        if (serviceResponse.isSuccess()){
            String logContent = "删除落地页模板【" + queryPageDetail.getResult().getPageTitle() + "-" + queryPageDetail.getResult().getPageName()+"】";
            GicLogRecordEvaluationContext.putAttribute("logContent", logContent);
            return RestResponse.successResult(serviceResponse.getResult());
        }
        GicLogRecordEvaluationContext.noWriteLog();
        return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
    }

    @RequestMapping(path = "/page/detail")
    public RestResponse<?> queryPageDetail(Long pageId){
        ServiceResponse<HmPageDTO> serviceResponse = pageApiService.queryPageDetail(pageId);
        return RestResponse.successResult(serviceResponse.getResult());
    }

    @RequestMapping(path = "/page/relation")
    public RestResponse<?> queryPageRelationList(PageRelationVO pageRelationVO){
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        HmPageRelationQDTO pageRelationQueryDTO = new HmPageRelationQDTO();
        pageRelationQueryDTO.setPageNum(pageRelationVO.getPageNum());
        pageRelationQueryDTO.setPageSize(pageRelationVO.getPageSize());
        pageRelationQueryDTO.setSearchText(pageRelationVO.getSearchContent());
        pageRelationQueryDTO.setPageId(pageRelationVO.getPageId());
        pageRelationQueryDTO.setEnterpriseId(loginUser.getEnterpriseId());
        pageRelationQueryDTO.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        ServiceResponse<Page<HmPageRelationDTO>> serviceResponse = pageApiService.queryPageRelation(pageRelationQueryDTO);
        return RestResponse.successResult(serviceResponse.getResult());
    }
}
