package org.jeecg.modules.hotelshop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.hotelconf.service.IHotelConfService;
import org.jeecg.modules.hotelmemberaccount.entity.HotelMemberAccount;
import org.jeecg.modules.hotelmemberaccount.service.IHotelMemberAccountService;
import org.jeecg.modules.hotelshop.entity.HotelShop;
import org.jeecg.modules.hotelshop.req.AuditShopReq;
import org.jeecg.modules.hotelshop.service.IHotelShopService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.hotelshoplog.entity.HotelShopLog;
import org.jeecg.modules.hotelshoplog.service.IHotelShopLogService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: hotel_shop
 * @Author: jeecg-boot
 * @Date:   2024-09-16
 * @Version: V1.0
 */
@Api(tags="hotel_shop")
@RestController
@RequestMapping("/hotelshop/hotelShop")
@Slf4j
public class HotelShopController extends JeecgController<HotelShop, IHotelShopService> {
	@Autowired
	private IHotelShopService hotelShopService;
	@Resource
	private IHanHaiMemberService hanHaiMemberService;
	@Resource
	private IHotelMemberAccountService hotelMemberAccountService;
	@Resource
	private IHotelShopLogService hotelShopLogService;
	@Resource
	private IHotelConfService hotelConfService;

	/**
	 * 分页列表查询
	 *
	 * @param hotelShop
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "hotel_shop-分页列表查询")
	@ApiOperation(value="hotel_shop-分页列表查询", notes="hotel_shop-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<HotelShop>> queryPageList(HotelShop hotelShop,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HotelShop> queryWrapper = QueryGenerator.initQueryWrapper(hotelShop, req.getParameterMap());
		Page<HotelShop> page = new Page<HotelShop>(pageNo, pageSize);
		IPage<HotelShop> pageList = hotelShopService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hotelShop
	 * @return
	 */
	@AutoLog(value = "hotel_shop-添加")
	@ApiOperation(value="hotel_shop-添加", notes="hotel_shop-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody HotelShop hotelShop) {
		HanHaiMember hanHaiMember = new HanHaiMember();
		hanHaiMember.setId(IdWorker.getIdStr());
		hanHaiMember.setPhone(hotelShop.getPhone());
		String salt = oConvertUtils.randomGen(8);
		hanHaiMember.setPasswordSalt(salt);
		String encrypt = PasswordUtil.encrypt(hotelShop.getPassword(), hotelShop.getPhone(), salt);
		hanHaiMember.setPassword(encrypt);
		hanHaiMember.setHeadImage(hotelShop.getPic());

		HotelMemberAccount hotelMemberAccount = new HotelMemberAccount();
		hotelMemberAccount.setUserId(hanHaiMember.getId());
		hotelShop.setUserId(hanHaiMember.getId());
		String shopDesc = hotelConfService.getConf("shopDesc");
		String[] split = StringUtils.split(shopDesc, "#");

		Long count = hotelShopService.lambdaQuery()
				.eq(HotelShop::getDelFlag, 0)
				.count();
		hotelShop.setDescInfo(split[0]+count+split[1]);

		hanHaiMemberService.save(hanHaiMember);
		hotelMemberAccountService.save(hotelMemberAccount);
		hotelShopService.save(hotelShop);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hotelShop
	 * @return
	 */
	@AutoLog(value = "hotel_shop-编辑")
	@ApiOperation(value="hotel_shop-编辑", notes="hotel_shop-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody HotelShop hotelShop) {
		if(StringUtils.isNotBlank(hotelShop.getPassword())){
			HanHaiMember hanHaiMember = hanHaiMemberService.lambdaQuery()
					.eq(HanHaiMember::getId, hotelShop.getUserId())
					.one();
			String salt = oConvertUtils.randomGen(8);
			String encrypt = PasswordUtil.encrypt(hotelShop.getPassword(), hotelShop.getPhone(), salt);
			HanHaiMember hanHaiMember1 = new HanHaiMember();
			hanHaiMember1.setId(hanHaiMember.getId());
			hanHaiMember1.setPassword(encrypt);
			hanHaiMember1.setPasswordSalt(salt);
			hanHaiMemberService.updateById(hanHaiMember1);
		}
		hotelShopService.updateById(hotelShop);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "hotel_shop-通过id删除")
	@ApiOperation(value="hotel_shop-通过id删除", notes="hotel_shop-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		hotelShopService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "hotel_shop-批量删除")
	@ApiOperation(value="hotel_shop-批量删除", notes="hotel_shop-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hotelShopService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "hotel_shop-通过id查询")
	@ApiOperation(value="hotel_shop-通过id查询", notes="hotel_shop-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<HotelShop> queryById(@RequestParam(name="id",required=true) String id) {
		HotelShop hotelShop = hotelShopService.getById(id);
		if(hotelShop==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hotelShop);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hotelShop
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HotelShop hotelShop) {
        return super.exportXls(request, hotelShop, HotelShop.class, "hotel_shop");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HotelShop.class);
    }


	 /**
	  *  审核
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "hotel_shop-审核")
	 @RequestMapping(value = "/audit", method = {RequestMethod.POST})
	 public Result<String> audit(@RequestBody AuditShopReq auditShopReq) {
		 HotelShop hotelShop = hotelShopService.lambdaQuery()
				 .eq(HotelShop::getId, auditShopReq.getId())
				 .one();

		 HanHaiMember haiMember = hanHaiMemberService.lambdaQuery()
				 .eq(HanHaiMember::getId, hotelShop.getUserId())
				 .one();

		 HotelMemberAccount hotelMemberAccount1 = hotelMemberAccountService.lambdaQuery()
				 .eq(HotelMemberAccount::getDelFlag, 0)
				 .eq(HotelMemberAccount::getUserId, haiMember.getId())
				 .one();

		 HotelShop hotelShop1 = new HotelShop();
		 hotelShop1.setId(hotelShop.getId());
		 if(auditShopReq.getAudit() == 0){
		 	String uid = IdWorker.getIdStr();
			 hotelShop1.setUserId(uid);
			 hotelShop1.setNewId(uid);
			 hotelShop1.setStatus(1);

			 String shopDesc = hotelConfService.getConf("shopDesc");
			 String[] split = StringUtils.split(shopDesc, "#");

			 Long count = hotelShopService.lambdaQuery()
					 .eq(HotelShop::getDelFlag, 0)
					 .count();
			 hotelShop1.setDescInfo(split[0]+count+split[1]);



			 hanHaiMemberService.updateUserId(haiMember.getId(),uid);
			 HotelMemberAccount hotelMemberAccount = new HotelMemberAccount();
			 hotelMemberAccount.setId(hotelMemberAccount1.getId());
			 hotelMemberAccount.setUserId(uid);
			 hotelMemberAccountService.updateById(hotelMemberAccount);

			 HotelShopLog hotelShopLog = new HotelShopLog();
			 hotelShopLog.setOldId(haiMember.getId());
			 hotelShopLog.setNewId(uid);
			 hotelShopLog.setShopId(hotelShop.getId());
			 hotelShopLogService.save(hotelShopLog);

		 }else{
			 hotelShop1.setStatus(2);
		 }
		 hotelShopService.updateById(hotelShop1);
		 return Result.OK("成功!");
	 }
}
