package org.jeecg.modules.msgTer.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.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.msgTechnician.entity.MsgTechnician;
import org.jeecg.modules.msgTechnician.service.IMsgTechnicianService;
import org.jeecg.modules.msgTer.entity.MsgTer;
import org.jeecg.modules.msgTer.service.IMsgTerService;

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.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: 技师入驻表
 * @Author: jeecg-boot
 * @Date:   2024-11-18
 * @Version: V1.0
 */
@Api(tags="技师入驻表")
@RestController
@RequestMapping("/msgTer/msgTer")
@Slf4j
public class MsgTerController extends JeecgController<MsgTer, IMsgTerService> {
	@Autowired
	private IMsgTerService msgTerService;
	@Autowired
	private IMsgTechnicianService msgTechnicianService;

	/**
	 * 分页列表查询
	 *
	 * @param msgTer
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "技师入驻表-分页列表查询")
	@ApiOperation(value="技师入驻表-分页列表查询", notes="技师入驻表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<MsgTer>> queryPageList(MsgTer msgTer,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<MsgTer> queryWrapper = QueryGenerator.initQueryWrapper(msgTer, req.getParameterMap());
		Page<MsgTer> page = new Page<MsgTer>(pageNo, pageSize);
		IPage<MsgTer> pageList = msgTerService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param msgTer
	 * @return
	 */
	@AutoLog(value = "技师入驻表-添加")
	@ApiOperation(value="技师入驻表-添加", notes="技师入驻表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody MsgTer msgTer) {
		msgTerService.save(msgTer);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param msgTer
	 * @return
	 */
	@AutoLog(value = "技师入驻表-编辑")
	@ApiOperation(value="技师入驻表-编辑", notes="技师入驻表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody MsgTer msgTer) {
		//用户标识不能为空
		if(oConvertUtils.isEmpty(msgTer.getUserId())){
			return Result.error("用户标识不能为空");
		}
		//查询该用户是否存在技师数据
		MsgTechnician msgTechnician = msgTechnicianService
				.lambdaQuery()
				.eq(MsgTechnician::getUserId,msgTer.getUserId())
				.one();
		if(msgTechnician!=null){
			return Result.error("当前用户已绑定技师账号，请更换账号重新申请");
		}

		//如果状态为审核通过
		if(msgTer.getState()==1){
			//查询该用户是否已经入驻
			MsgTer one = msgTerService.lambdaQuery()
					.eq(MsgTer::getUserId, msgTer.getUserId())
					.eq(MsgTer::getState, 1)
					.one();
			if(one!=null){
				return Result.error("当前用户已入驻，请勿重复入驻");
			}

			//把MsgTer的数据写入技师表
			MsgTechnician msgTechnician1 = new MsgTechnician();
			msgTechnician1.setImage(msgTer.getImage());
			msgTechnician1.setTitle(msgTer.getName());
			msgTechnician1.setIcon("https://dianpin-img.xzaiyp.top/upload/WechatIMG1666_1719811927700.jpg");
			msgTechnician1.setUserId(msgTer.getUserId());
			msgTechnician1.setLevel(0);
			msgTechnician1.setScore(0);
			msgTechnician1.setNotes("新人多关照");
			msgTechnician1.setIsVip("官方认证");
			msgTechnician1.setKg("-");
			msgTechnician1.setCm("-");
			msgTechnician1.setArea("-");
			msgTechnician1.setCity("-");
			msgTechnician1.setCounty("-");
			msgTechnician1.setRealOk("Y");
			msgTechnician1.setSex(msgTer.getSex());
			msgTechnician1.setExperience("99");
			msgTechnician1.setIsSb(100);
			msgTechnician1.setIsFw(10);
			msgTechnician1.setCollectNum(0);
			msgTechnician1.setImages(msgTer.getIdImage()+","+msgTer.getIdImage2());
			msgTechnician1.setPinNum(0);
			msgTechnician1.setUseTime("-");
			msgTechnician1.setLongitude("113.544");
			msgTechnician1.setIsKm(0);
			msgTechnician1.setLatitude("34.763");
			msgTechnician1.setBookable("Y");
			msgTechnician1.setIsCx("Y");
			msgTechnician1.setSetKm(0);
			msgTechnician1.setIsTest(0);
			msgTechnician1.setSetKmOpen("N");
			msgTechnician1.setHealthCertificate("-");
			msgTechnician1.setBusinessLicense("-");
			msgTechnicianService.save(msgTechnician1);
		}
		msgTerService.updateById(msgTer);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "技师入驻表-通过id删除")
	@ApiOperation(value="技师入驻表-通过id删除", notes="技师入驻表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		msgTerService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "技师入驻表-批量删除")
	@ApiOperation(value="技师入驻表-批量删除", notes="技师入驻表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.msgTerService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "技师入驻表-通过id查询")
	@ApiOperation(value="技师入驻表-通过id查询", notes="技师入驻表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<MsgTer> queryById(@RequestParam(name="id",required=true) String id) {
		MsgTer msgTer = msgTerService.getById(id);
		if(msgTer==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(msgTer);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param msgTer
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MsgTer msgTer) {
        return super.exportXls(request, msgTer, MsgTer.class, "技师入驻表");
    }

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

}
