/*
 * Copyright 2014-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.docksidestage.app.web.member;

import java.time.LocalDateTime;

import javax.annotation.Resource;

import org.dbflute.cbean.result.PagingResultBean;
import org.dbflute.optional.OptionalThing;
import org.docksidestage.app.web.base.HarborBaseAction;
import org.docksidestage.dbflute.exbhv.MemberBhv;
import org.docksidestage.dbflute.exentity.Member;
import org.lastaflute.web.Execute;
import org.lastaflute.web.response.HtmlResponse;

/**
 * @author jflute
 */
public class MemberListAction extends HarborBaseAction {

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    @Resource
    private MemberBhv memberBhv;

    // ===================================================================================
    //                                                                             Execute
    //                                                                             =======
    @Execute
    public HtmlResponse index(OptionalThing<Integer> pageNumber, MemberSearchForm form) {
        validate(form, messages -> {} , () -> {
            return asHtml(path_Member_MemberListJsp);
        });
        PagingResultBean<Member> page = selectMemberPage(pageNumber.orElse(1), form);
        PagingResultBean<MemberSearchRowBean> beans = page.mappingList(member -> {
            return mappingToBean(member);
        });
        return asHtml(path_Member_MemberListJsp).renderWith(data -> {
            data.register("beans", beans);
            registerPagingNavi(data, page, form);
        });
    }

    // ===================================================================================
    //                                                                              Select
    //                                                                              ======
    protected PagingResultBean<Member> selectMemberPage(int pageNumber, MemberSearchForm form) {
        return memberBhv.selectPage(cb -> {
            cb.setupSelect_MemberStatus();
            cb.specify().derivedPurchase().count(purchaseCB -> {
                purchaseCB.specify().columnPurchaseId();
            } , Member.ALIAS_purchaseCount);
            if (isNotEmpty(form.memberName)) {
                cb.query().setMemberName_LikeSearch(form.memberName, op -> op.likeContain());
            }
            if (isNotEmpty(form.purchaseProductName) || form.unpaid) {
                cb.query().existsPurchase(purchaseCB -> {
                    if (isNotEmpty(form.purchaseProductName)) {
                        purchaseCB.query().queryProduct().setProductName_LikeSearch(form.purchaseProductName, op -> op.likeContain());
                    }
                    if (form.unpaid) {
                        purchaseCB.query().setPaymentCompleteFlg_Equal_False();
                    }
                });
            }
            if (form.memberStatus != null) {
                cb.query().setMemberStatusCode_Equal_AsMemberStatus(form.memberStatus);
            }
            OptionalThing<LocalDateTime> fromDate = toDateTime(form.formalizedFrom);
            OptionalThing<LocalDateTime> toDate = toDateTime(form.formalizedTo);
            if (fromDate.isPresent() || toDate.isPresent()) {
                cb.query().setFormalizedDatetime_FromTo(fromDate.orElse(null), toDate.orElse(null), op -> {
                    op.compareAsDate().allowOneSide();
                });
            }
            cb.query().addOrderBy_UpdateDatetime_Desc();
            cb.query().addOrderBy_MemberId_Asc();
            cb.paging(getPagingPageSize(), pageNumber);
        });
    }

    // ===================================================================================
    //                                                                             Mapping
    //                                                                             =======
    private MemberSearchRowBean mappingToBean(Member member) {
        MemberSearchRowBean bean = new MemberSearchRowBean();
        bean.memberId = member.getMemberId();
        bean.memberName = member.getMemberName();
        member.getMemberStatus().alwaysPresent(status -> {
            bean.memberStatusName = status.getMemberStatusName();
        });
        bean.formalizedDate = toDate(member.getFormalizedDatetime()).orElse(null);
        bean.updateDatetime = toStringDateTime(member.getUpdateDatetime()).get();
        bean.withdrawalMember = member.isMemberStatusCodeWithdrawal();
        bean.purchaseCount = member.getPurchaseCount();
        return bean;
    }
}

@@ -1056,16 +1056,65 @@
 esponse;
+%0Aimport org.lastaflute.web.response.JsonResponse;
 %0A%0A/**%0A *
@@ -2399,32 +2399,612 @@
      %7D);%0A    %7D%0A%0A
+    @Execute%0A    public JsonResponse%3CPagingResultBean%3CMemberSearchRowBean%3E%3E indexJson(OptionalThing%3CInteger%3E pageNumber, MemberSearchForm form) %7B%0A        validate(form, messages -%3E %7B%7D , () -%3E %7B%0A            return JsonResponse.asEmptyBody().httpStatus(400);%0A        %7D);%0A        Integer pageNumberValue = pageNumber.orElse(1);%0A        PagingResultBean%3CMember%3E page = selectMemberPage(pageNumberValue, form);%0A%0A        PagingResultBean%3CMemberSearchRowBean%3E bean = page.mappingList(member -%3E %7B%0A            return mappingToBean(member);%0A        %7D);%0A%0A        return asJson(bean);%0A    %7D%0A%0A
     // =========

implement MemberList Action