package com.hzs.member.base.controller.manage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.base.param.MemberServiceCodeParam;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.member.base.vo.MemberServiceCodeVo;
import com.hzs.system.base.ICountryServiceApi;
import com.hzs.system.base.dto.CountryDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @BelongsProject: hzs_cloud
 * @BelongsPackage: com.hzs.member.base.controller.manager
 * @Author: yh
 * @CreateTime: 2024-07-09  11:20
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("/manage/member-service-code")
public class MemberServiceCodeController extends BaseController {

    @Autowired
    private ICuMemberService memberService;

    @DubboReference
    ICountryServiceApi countryServiceApi;

    /**
     * 会员服务编号列表
     */
    @GetMapping("list")
    public TableDataInfo list(MemberServiceCodeParam memberServiceCodeParam) {
        startPage();
        memberServiceCodeParam.setPkCountry(SecurityUtils.getPkCountry());
        List<MemberServiceCodeVo> memberList = memberService.queryMemberServiceCodeList(memberServiceCodeParam);
        return getDataTable(memberList);
    }

    /**
     * 会员服务编号导出
     */
    @PostMapping("export")
    public void export(MemberServiceCodeParam memberServiceCodeParam, HttpServletResponse response) {
        memberServiceCodeParam.setPkCountry(SecurityUtils.getPkCountry());
        List<MemberServiceCodeVo> memberList = memberService.queryMemberServiceCodeList(memberServiceCodeParam);
        ExcelUtil<MemberServiceCodeVo> util = new ExcelUtil<>(MemberServiceCodeVo.class);
        util.exportExcel(response, memberList, "商品信息导出");
    }

    /**
     * 添加
     *
     * @return
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody MemberServiceCodeParam memberServiceCodeParam) {
        R<CountryDTO> countryDto = countryServiceApi.getCountry(SecurityUtils.getPkCountry());
        CountryDTO country = countryDto.getData();


        QueryWrapper<CuMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_SETTLE_COUNTRY", SecurityUtils.getPkCountry());
        queryWrapper.eq("MEMBER_CODE", memberServiceCodeParam.getMemberCode());
        CuMember member = memberService.getOne(queryWrapper);
        if (member == null) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.MEMBER_DOES_NOT_EXITS));
        }
        if (StringUtils.isNotBlank(member.getServiceCode())) {
            return AjaxResult.error(TransactionUtils.getContent(MemberMsgConstants.EMPTY_MEMBER_REPEAT));
        }

        // 生成新编号
        String serviceCode = memberService.getMaxServiceCode(country.getPkId());
        Integer code = null;
        if (StringUtils.isNotBlank(serviceCode)) {
            code = Integer.parseInt(serviceCode.replaceAll("[^\\d]", "")) + 1;
        } else {
            code = 1;
        }

        UpdateWrapper<CuMember> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("MEMBER_CODE", memberServiceCodeParam.getMemberCode());
        updateWrapper.set("SERVICE_CODE", country.getCode() + String.format("%05d", code));
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("MODIFIED_TIME", DateUtils.currentDate());
        memberService.update(updateWrapper);


        return AjaxResult.success();
    }

    /**
     * 删除
     *
     * @param memberCode
     * @return
     */
    @GetMapping("remove")
    public AjaxResult remove(String memberCode) {
        memberService.updateMemberCodeByServiceCodeBlack(memberCode);
        return AjaxResult.success();
    }

    /**
     * 根据编号查询会员基本信息
     *
     * @param memberCode
     * @return
     */
    @GetMapping("member-detail")
    public AjaxResult memberDetail(String memberCode) {
        return AjaxResult.success(memberService.getMember(memberCode));
    }

}
