package com.hx.medical.controller.admin; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import com.hx.exception.LoginException; import com.hx.exception.TipsException; import com.hx.medical.annotation.Authority; import com.hx.medical.common.AuthConstants; import com.hx.medical.common.Constants; import com.hx.medical.config.SystemConfig; import com.hx.medical.config.TengXunConfig; import com.hx.medical.dao.mapper.ShopTypeMapper; import com.hx.medical.dao.mapper.ShopTypeRelationMapper; import com.hx.medical.model.*; import com.hx.medical.service.ShopWeekDayService; import com.hx.medical.service.SystemParameterService; import com.hx.medical.service.tool.ShopImportExcel; import com.hx.util.*; import net.sf.json.JSONArray; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.hx.common.BaseController; import com.hx.mybatisTool.SqlSentence; import com.hx.medical.dao.mapper.ShopMapper; import com.hx.medical.service.ShopService; import com.hx.resultTool.Result; import net.sf.json.JSONObject; import org.springframework.web.multipart.MultipartFile; @RestController @RequestMapping("/shop") public class ShopController extends BaseController { public static final String [] WEEK_ARR = {"周零", "周一", "周二", "周三", "周四", "周五", "周六", "周日"}; @Resource private ShopService service; @Resource private SystemParameterService systemParameterService; @Resource private ShopWeekDayService shopWeekDayService; @Resource private SystemConfig systemConfig; @Resource private TengXunConfig tengXunConfig; /**列表*/ @Authority(tag = AuthConstants.Shop.SEE, opName = "店铺列表") @RequestMapping("/list") public Result listData(Integer pageNum,Integer pageSize, Shop shop) { //分页插件 PageHelper.startPage(pageNum==null?1:pageNum, pageSize==null?20:pageSize, " syntheticalNum DESC,createTime desc"); SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); StringBuffer sb = new StringBuffer(); sqlSentence.setM(map); map.put("isDel", BaseEntity.NO); sb.append(" select s.* from shop s "); sb.append(" where s.isDel = #{m.isDel} "); if(!StringUtils.isEmpty(shop.getNumberNo())){ sb.append(" and s.numberNo like #{m.numberNo} "); map.put("numberNo", "%" + shop.getNumberNo() + "%"); } if(!StringUtils.isEmpty(shop.getName())){ sb.append(" and s.name like #{m.name} "); map.put("name", "%" + shop.getName() + "%"); } if(!StringUtils.isEmpty(shop.getLinkTel())){ sb.append(" and s.linkTel like #{m.linkTel} "); map.put("linkTel", "%" + shop.getLinkTel() + "%"); } sqlSentence.setSqlSentence(sb.toString()); List shops = service.selectList(sqlSentence); //解密剩余金额 // shops.forEach(s ->{ // if(!StringUtils.isEmpty(s.getWalletSurpTotal())){ // s.setWalletSurpTotal(AesUtil.aesDecryp(s.getWalletSurpTotal())); // } // }); PageInfo pageInfo = new PageInfo(shops); Map data = new HashMap<>(); data.put("pageNum",pageInfo.getPageNum()); data.put("pageSize",pageInfo.getPageSize()); data.put("total",pageInfo.getTotal()); data.put("isLastPage",pageInfo.isIsLastPage()); data.put("list",pageInfo.getList()); return Result.success(data); } /**新增*/ @Authority(tag = AuthConstants.Shop.ADD, opName = "新增店铺") @RequestMapping("/add") public Result addData(Shop shop, String shopWeekDayStr, String typeIds) { if(StringUtils.isNull(typeIds)){ throw new TipsException("店铺分类数据为空"); } JSONArray typeArr = new JSONArray(); try{ typeArr = JSONArray.fromObject(typeIds); }catch (Exception e){ throw new TipsException("店铺分类数据格式错误!"); } SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); map.put("isDel", BaseEntity.NO); int count = 0; //判断店铺编号是否重复 if(!StringUtils.isEmpty(shop.getNumberNo())){ sqlSentence.setSqlSentence(" isDel = #{m.isDel} and numberNo = #{m.numberNo} "); map.put("numberNo", shop.getNumberNo()); count = service.selectCount(sqlSentence); if(count > 0){ throwParamException("店铺编号已存在"); } }else{ //系统自定义店铺编号 shop.setNumberNo(systemParameterService.createNumberNo(Constants.SHOP_NO)); } //判断店铺名是否重复 sqlSentence.setSqlSentence(" isDel = #{m.isDel} and name = #{m.name} "); map.put("name", shop.getName()); count = service.selectCount(sqlSentence); if(count > 0){ throwParamException("店铺名称已存在"); } //没有填就自动获取填充经纬度 if(StringUtils.isNull(shop.getLat()) || StringUtils.isNull(shop.getLng())){ //获取经纬度 String[] strs = TengXunMapUtil.getLocation(shop.showDetail(), tengXunConfig.getTengXunMpKey(), tengXunConfig.getTengXunMpSign()); if(strs != null && strs.length == 2){ shop.setLng(strs[0]); shop.setLat(strs[1]); }else{ shop.setLng("0"); shop.setLat("0"); } } //通过数组生成店铺营业星期几 List shopWeekDayList = handleShopWeekDayStr(shopWeekDayStr); //店铺分类 List typeRelationList = new ArrayList<>(); for(int i = 0;i handleShopWeekDayStr(String shopWeekDayStr){ List list = new ArrayList<>(); if(StringUtils.isEmpty(shopWeekDayStr)){ return list; } String [] arr = shopWeekDayStr.split(","); Integer weekDayNum = 0; for(String s : arr){ weekDayNum = Integer.parseInt(s); list.add(new ShopWeekDay(weekDayNum, WEEK_ARR[weekDayNum])); } return list; } /**获取数据*/ @Authority(tag = AuthConstants.Shop.SEE, opName = "获取店铺信息") @RequestMapping("/see") public Result editeData(String id) { if(StringUtils.isEmpty(id)){ throwParamException("id为空"); } Shop shop = service.selectOneByKeyBlob(id); if(shop == null){ throwParamException("找不到店铺"); } SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); map.put("isDel", BaseEntity.NO); map.put("id", id); sqlSentence.setSqlSentence(" select weekDayNum from shop_week_day where isDel = #{m.isDel} and shopId = #{m.id} "); List list = shopWeekDayService.selectList(sqlSentence); //获取店铺分类 map.clear(); map.put("isDel",ShopTypeRelation.NO); map.put("shopId",shop.getId()); sqlSentence.sqlSentence("SELECT st.id,st.name FROM shop_type_relation str JOIN shop_type st ON st.id = str.shopTypeId WHERE str.isDel = #{m.isDel} AND str.shopId = #{m.shopId}",map); List> typeList = commonService.selectListMap(ShopTypeRelationMapper.class,sqlSentence); JSONObject json = new JSONObject(); json.put("shop",shop); json.put("weekList", list); json.put("typeList", typeList); return Result.success(json); } /**修改数据*/ @Authority(tag = AuthConstants.Shop.EDIT, opName = "编辑店铺") @RequestMapping("/edit") public Result updateData(Shop shop, String shopWeekDayStr,String typeIds) { if(StringUtils.isEmpty(shop.getId())){ throwParamException("id为空"); } if(StringUtils.isNull(typeIds)){ throw new TipsException("店铺分类数据为空"); } JSONArray typeArr = new JSONArray(); try{ typeArr = JSONArray.fromObject(typeIds); }catch (Exception e){ throw new TipsException("店铺分类数据格式错误!"); } SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); map.put("isDel", BaseEntity.NO); int count = 0; //判断店铺名是否重复 sqlSentence.setSqlSentence(" isDel = #{m.isDel} and name = #{m.name} and id != #{m.id}"); map.put("name", shop.getName()); map.put("id", shop.getId()); count = service.selectCount(sqlSentence); if(count > 0){ throwParamException("店铺名称已存在"); } //判断店铺编号是否重复 if(!StringUtils.isEmpty(shop.getNumberNo())){ sqlSentence.setSqlSentence(" isDel = #{m.isDel} and numberNo = #{m.numberNo} and id != #{m.id} "); map.put("numberNo", shop.getNumberNo()); count = service.selectCount(sqlSentence); if(count > 0){ throwParamException("店铺编号已存在"); } }else{ //系统自定义店铺编号 shop.setNumberNo(systemParameterService.createNumberNo(Constants.SHOP_NO)); } if(StringUtils.isNull(shop.getLat()) || StringUtils.isNull(shop.getLng())){ //获取经纬度 String[] strs = TengXunMapUtil.getLocation(shop.showDetail(), tengXunConfig.getTengXunMpKey(), tengXunConfig.getTengXunMpSign()); if(strs != null && strs.length == 2){ shop.setLng(strs[0]); shop.setLat(strs[1]); }else{ shop.setLng("0"); shop.setLat("0"); } } //通过数组生成店铺营业星期几 List shopWeekDayList = handleShopWeekDayStr(shopWeekDayStr); //店铺分类 List typeRelationList = new ArrayList<>(); for(int i = 0;i map = new HashMap<>(); sqlSentence.setM(map); map.put("isUp", isUp); map.put("id", id); sqlSentence.setSqlSentence(" isUp = #{m.isUp} where id = #{m.id}"); service.updateWhere(sqlSentence); return Result.success(); } /** 推广,综合 */ @RequestMapping("/synthetical") @Authority(tag = AuthConstants.Shop.EDIT, opName = "编辑是否推广") public Result synthetical(String id){ if(StringUtils.isEmpty(id)){ throw new TipsException("店铺标识为空!"); } Shop shop = service.selectOneByKeyBlob(id); if(shop == null){ throw new TipsException("店铺标识错误!"); } SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); map.put("syntheticalNum",shop.getSyntheticalNum() == Shop.NO?Shop.YES:Shop.NO); map.put("id", id); sqlSentence.setSqlSentence(" syntheticalNum = #{m.syntheticalNum} where id = #{m.id}"); service.updateWhere(sqlSentence); return Result.success(); } /** 获取下拉店铺数据 */ @RequestMapping("/getSelectShopData") public Result getSelectShopData(String name){ SqlSentence sqlSentence = new SqlSentence(); Map map = new HashMap<>(); sqlSentence.setM(map); map.put("isDel", BaseEntity.NO); map.put("isUp", BaseEntity.YES); sqlSentence.setSqlSentence(" select id, name from shop where isDel = #{m.isDel} and isUp = #{m.isUp} order by ranking asc"); List> list = service.selectListMap(sqlSentence); return Result.success(list); } /** 复制店铺 */ @RequestMapping("/co") @Authority(tag = AuthConstants.Shop.ADD, opName = "复制店铺") public Result copyShop(String numberNo) { SysAdmin sysAdmin = (SysAdmin) getRequestAttribute(Constants.LOGIN_ADMIN); if(sysAdmin == null){ throw new LoginException("请先登录!"); } if(StringUtils.isNull(numberNo)){ throw new TipsException("请填写复制的店铺编码"); } SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); values.put("isDel",Shop.NO); values.put("numberNo",numberNo); sqlSentence.sqlSentence("SELECT * FROM shop WHERE isDel = #{m.isDel} AND numberNo = #{m.numberNo}",values); Shop shop = service.selectOne(sqlSentence); if(shop == null){ throw new TipsException("填写的复制的店铺编码错误!"); } String code = systemParameterService.createNumberNo(Constants.SHOP_NO); service.copyShop(shop,code,sysAdmin); return Result.success(); } /** 导入excel数据 */ @RequestMapping("/importExcel") @Authority(tag = AuthConstants.Shop.ADD, opName = "导入店铺") public Result importExcel(MultipartFile file) throws Exception { if(file == null){ throw new TipsException("请选择导入文件!"); } List> dataList = ExcelUtil.getExcelDataCompatible(file,false); service.importShop(dataList); return Result.success(); } }