|  |  |  | 
|---|
|  |  |  | package com.zy.crm.manager.controller; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONArray; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.mapper.EntityWrapper; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.core.common.DateUtils; | 
|---|
|  |  |  | import com.core.common.R; | 
|---|
|  |  |  | import com.core.domain.KeyValueVo; | 
|---|
|  |  |  | import com.core.exception.CoolException; | 
|---|
|  |  |  | import com.zy.crm.common.model.SettleDto; | 
|---|
|  |  |  | import com.zy.crm.common.web.BaseController; | 
|---|
|  |  |  | import com.zy.crm.manager.entity.Item; | 
|---|
|  |  |  | import com.zy.crm.manager.entity.PriQuoteBudget; | 
|---|
|  |  |  | import com.zy.crm.manager.entity.PriSales; | 
|---|
|  |  |  | import com.zy.crm.manager.service.ItemService; | 
|---|
|  |  |  | import com.zy.crm.manager.service.PriQuoteBudgetService; | 
|---|
|  |  |  | import com.zy.crm.manager.service.PriSalesService; | 
|---|
|  |  |  | import com.zy.crm.manager.entity.*; | 
|---|
|  |  |  | import com.zy.crm.manager.service.*; | 
|---|
|  |  |  | import com.zy.crm.system.entity.User; | 
|---|
|  |  |  | import com.zy.crm.system.service.UserService; | 
|---|
|  |  |  | import org.apache.poi.xssf.usermodel.XSSFSheet; | 
|---|
|  |  |  | import org.apache.poi.xssf.usermodel.XSSFWorkbook; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | 
|---|
|  |  |  | private PriQuoteBudgetService priQuoteBudgetService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ItemService itemService; | 
|---|
|  |  |  | private PlanService planService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private PriSalesService priSalesService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private PriQuoteService priQuoteService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private UserService userService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/priQuoteBudget/{id}/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | public R get(@PathVariable("id") String id) { | 
|---|
|  |  |  | return R.ok(priQuoteBudgetService.selectById(String.valueOf(id))); | 
|---|
|  |  |  | PriQuoteBudget priQuoteBudget = priQuoteBudgetService.selectById(String.valueOf(id)); | 
|---|
|  |  |  | assert priQuoteBudget != null; | 
|---|
|  |  |  | JSONObject resultObj = JSON.parseObject(JSON.toJSONString(priQuoteBudget)); | 
|---|
|  |  |  | // 步骤条相关 | 
|---|
|  |  |  | resultObj.put("step", priQuoteBudget.getSettle() == 4 ? 0 : priQuoteBudget.getSettle() + 1); | 
|---|
|  |  |  | return R.ok().add(resultObj); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/priQuoteBudget/list/auth") | 
|---|
|  |  |  | 
|---|
|  |  |  | @RequestParam(required = false)String condition, | 
|---|
|  |  |  | @RequestParam Map<String, Object> param){ | 
|---|
|  |  |  | EntityWrapper<PriQuoteBudget> wrapper = new EntityWrapper<>(); | 
|---|
|  |  |  | wrapper.setSqlSelect("id,title,create_time as createTime,filepath,item_id as itemId,order_num as orderNum,template_name as templateName,user_id as userId,update_user_id as updateUserId,update_time as updateTime,in_order_num as inOrderNum,member_id as memberId"); | 
|---|
|  |  |  | wrapper.in("member_id", getUserRoleBelongsToUserId()); | 
|---|
|  |  |  | wrapper.setSqlSelect("id,title,create_time as createTime,settle,filepath,item_id as itemId,order_num as orderNum,template_name as templateName,user_id as userId,update_user_id as updateUserId,update_time as updateTime,in_order_num as inOrderNum,member_id as memberId"); | 
|---|
|  |  |  | //        wrapper.in("member_id", getUserRoleBelongsToUserId()); | 
|---|
|  |  |  | excludeTrash(param); | 
|---|
|  |  |  | convert(param, wrapper); | 
|---|
|  |  |  | allLike(PriQuoteBudget.class, param.keySet(), wrapper, condition); | 
|---|
|  |  |  | wrapper.or().eq("member_id",getUserId()); | 
|---|
|  |  |  | if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));} | 
|---|
|  |  |  | return R.ok(priQuoteBudgetService.selectPage(new Page<>(curr, limit), wrapper)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | private <T> void convert(Map<String, Object> map, EntityWrapper<T> wrapper){ | 
|---|
|  |  |  | boolean signUserId = false; | 
|---|
|  |  |  | boolean signDeptId = false; | 
|---|
|  |  |  | boolean signHostId = false; | 
|---|
|  |  |  | for (Map.Entry<String, Object> entry : map.entrySet()){ | 
|---|
|  |  |  | if (entry.getKey().equals("dept_id")){ | 
|---|
|  |  |  | signDeptId = true; | 
|---|
|  |  |  | if (String.valueOf(entry.getValue()).equals("19")){ | 
|---|
|  |  |  | signHostId = true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (Map.Entry<String, Object> entry : map.entrySet()){ | 
|---|
|  |  |  | 
|---|
|  |  |  | if (!signUserId && !signDeptId){ | 
|---|
|  |  |  | wrapper.eq("user_id", getUserId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (signHostId){ | 
|---|
|  |  |  | wrapper.or().eq("host_id",1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/priQuoteBudget/add/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | @ManagerAuth(memo = "添加报价预算单") | 
|---|
|  |  |  | public R add(@RequestBody Map<String,Object> map) { | 
|---|
|  |  |  | PriSales priSales = priSalesService.selectById(Long.parseLong(map.get("priSalesId").toString())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | priQuoteBudget.setTitle(map.get("title").toString()); | 
|---|
|  |  |  | priQuoteBudget.setTemplateName(priSales.getTemplateName()); | 
|---|
|  |  |  | priQuoteBudget.setSheetData(map.get("sheetData").toString()); | 
|---|
|  |  |  | priQuoteBudget.setPriSalesId(Long.parseLong(map.get("priSalesId").toString())); | 
|---|
|  |  |  | priQuoteBudget.setItemId(priSales.getItemId()); | 
|---|
|  |  |  | //保存产品费用明细订单号 | 
|---|
|  |  |  | priQuoteBudget.setOrderNum(priSales.getInOrderNum()); | 
|---|
|  |  |  | 
|---|
|  |  |  | priQuoteBudget.setUpdateTime(new Date()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //业务员 | 
|---|
|  |  |  | Item item = itemService.selectById(priQuoteBudget.getItemId()); | 
|---|
|  |  |  | priQuoteBudget.setMemberId(item.getMember()); | 
|---|
|  |  |  | Plan plan = planService.selectById(priQuoteBudget.getItemId()); | 
|---|
|  |  |  | priQuoteBudget.setMemberId(plan.getUserId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | priQuoteBudget.setSettle(0); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | User manager = userService.getDeptManager(getHostId(), getUser().getDeptId());        // 获取部门领导 | 
|---|
|  |  |  | List<String> initNames = new ArrayList<>(); | 
|---|
|  |  |  | initNames.add("创建报价预算单模板"); | 
|---|
|  |  |  | initNames.add("提交报价预算单"); | 
|---|
|  |  |  | initNames.add("部门经理审核"); | 
|---|
|  |  |  | initNames.add("业务员确认"); | 
|---|
|  |  |  | priQuoteBudget.setSettleMsg(JSON.toJSONString(SettleDto.initSalesPricing(manager,getUser(),getUser(),initNames,3))); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //设置项目流程 | 
|---|
|  |  |  | item.setStep(4); | 
|---|
|  |  |  | itemService.updateById(item); | 
|---|
|  |  |  | plan.setStep(4); | 
|---|
|  |  |  | planService.updateById(plan); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | priQuoteBudgetService.insert(priQuoteBudget); | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/priQuoteBudget/addOther/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | @ManagerAuth(memo = "另存报价预算单") | 
|---|
|  |  |  | public R addOther(@RequestBody Map<String,Object> map) { | 
|---|
|  |  |  | PriQuoteBudget quote = priQuoteBudgetService.selectById(Long.parseLong(map.get("id").toString())); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | priQuoteBudget.setUpdateTime(new Date()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //业务员 | 
|---|
|  |  |  | Item item = itemService.selectById(priQuoteBudget.getItemId()); | 
|---|
|  |  |  | priQuoteBudget.setMemberId(item.getMember()); | 
|---|
|  |  |  | Plan plan = planService.selectById(priQuoteBudget.getItemId()); | 
|---|
|  |  |  | priQuoteBudget.setMemberId(plan.getUserId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | priQuoteBudgetService.insert(priQuoteBudget); | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/priQuoteBudget/update/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | @ManagerAuth(memo = "更新报价预算单") | 
|---|
|  |  |  | public R update(@RequestBody Map<String,Object> map){ | 
|---|
|  |  |  | PriQuoteBudget priQuoteBudget = priQuoteBudgetService.selectById(Long.parseLong(map.get("id").toString())); | 
|---|
|  |  |  | priQuoteBudget.setTitle(map.get("title").toString()); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/priQuoteBudget/updateForm/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | @ManagerAuth(memo = "更新报价预算单") | 
|---|
|  |  |  | public R updateForm(Long id,String templateName,String title){ | 
|---|
|  |  |  | PriQuoteBudget priQuoteBudget = priQuoteBudgetService.selectById(id); | 
|---|
|  |  |  | priQuoteBudget.setTemplateName(templateName); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/priQuoteBudget/delete/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | @ManagerAuth(memo = "删除报价预算单") | 
|---|
|  |  |  | public R delete(Long[] ids){ | 
|---|
|  |  |  | if (Cools.isEmpty(ids)){ | 
|---|
|  |  |  | return R.error(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (Long id : ids){ | 
|---|
|  |  |  | int  priQuote= priQuoteService.selectCount(new EntityWrapper<PriQuote>().eq("ori_quote_budget_id", id)); | 
|---|
|  |  |  | if (priQuote!=0){ | 
|---|
|  |  |  | return R.error("存在关联的报价单,禁止删除!!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | priQuoteBudgetService.deleteBatchIds(Arrays.asList(ids)); | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | 
|---|
|  |  |  | public R query(String condition) { | 
|---|
|  |  |  | EntityWrapper<PriQuoteBudget> wrapper = new EntityWrapper<>(); | 
|---|
|  |  |  | wrapper.like("id", condition); | 
|---|
|  |  |  | wrapper.in("member_id", getUserRoleBelongsToUserId()); | 
|---|
|  |  |  | wrapper.in("user_id", getUserRoleBelongsToUserId()); | 
|---|
|  |  |  | Page<PriQuoteBudget> page = priQuoteBudgetService.selectPage(new Page<>(0, 10), wrapper); | 
|---|
|  |  |  | List<Map<String, Object>> result = new ArrayList<>(); | 
|---|
|  |  |  | for (PriQuoteBudget priQuoteBudget : page.getRecords()){ | 
|---|
|  |  |  | Map<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | map.put("id", priQuoteBudget.getId()); | 
|---|
|  |  |  | map.put("value", priQuoteBudget.getInOrderNum() + "/" + priQuoteBudget.getItemId$() + "/" + priQuoteBudget.getMemberId$()); | 
|---|
|  |  |  | map.put("value", priQuoteBudget.getInOrderNum() + "/" + priQuoteBudget.getPlanId$() + "/" + priQuoteBudget.getMemberId$() + "/" + priQuoteBudget.getTemplateName()); | 
|---|
|  |  |  | result.add(map); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(result); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping(value = "/priQuoteBudget/approval/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | public R approvalBusinessTrip(@RequestParam Long priQuoteBudgetId, | 
|---|
|  |  |  | @RequestParam(required = false) Long plannerId){ | 
|---|
|  |  |  | PriQuoteBudget priQuoteBudget = priQuoteBudgetService.selectById(priQuoteBudgetId); | 
|---|
|  |  |  | assert priQuoteBudget != null; | 
|---|
|  |  |  | Date now = new Date(); | 
|---|
|  |  |  | switch (priQuoteBudget.getSettle()) { | 
|---|
|  |  |  | case 0: | 
|---|
|  |  |  | User user2 = userService.selectById(priQuoteBudget.getUserId()); | 
|---|
|  |  |  | User manager1 = userService.getDeptManager(getHostId(), user2.getDeptId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (Cools.isEmpty(getUser())) { | 
|---|
|  |  |  | return R.error("抱歉,您没有提交的权限"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!getUserId().equals(getUser().getId())) { | 
|---|
|  |  |  | return R.error("抱歉,您没有提交的权限"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 修改 settle 步骤数据 | 
|---|
|  |  |  | List<SettleDto> list1 = JSON.parseArray(priQuoteBudget.getSettleMsg(), SettleDto.class); | 
|---|
|  |  |  | for (SettleDto dto : list1) { | 
|---|
|  |  |  | switch (dto.getStep()) { | 
|---|
|  |  |  | case 0: | 
|---|
|  |  |  | dto.setCurr(Boolean.FALSE); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 1: | 
|---|
|  |  |  | dto.setCurr(Boolean.TRUE); | 
|---|
|  |  |  | dto.setMsg(getUser().getNickname() + "提交完成"); | 
|---|
|  |  |  | dto.setTime(DateUtils.convert(now)); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | priQuoteBudget.setSettleMsg(JSON.toJSONString(list1)); | 
|---|
|  |  |  | // 修改规划单状态 | 
|---|
|  |  |  | priQuoteBudget.setSettle(1); | 
|---|
|  |  |  | priQuoteBudget.setUpdateUserId(getUserId()); | 
|---|
|  |  |  | priQuoteBudget.setUpdateTime(now); | 
|---|
|  |  |  | priQuoteBudget.setMemberId(manager1.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!priQuoteBudgetService.updateById(priQuoteBudget)) { | 
|---|
|  |  |  | throw new CoolException("提交失败,请联系管理员"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 1: | 
|---|
|  |  |  | // 本部门经理审核 | 
|---|
|  |  |  | User user = userService.selectById(priQuoteBudget.getUserId()); | 
|---|
|  |  |  | User manager = userService.getDeptManager(getHostId(), user.getDeptId()); | 
|---|
|  |  |  | if (manager.getId().equals(getUserId())) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 修改 settle 步骤数据 | 
|---|
|  |  |  | List<SettleDto> list = JSON.parseArray(priQuoteBudget.getSettleMsg(), SettleDto.class); | 
|---|
|  |  |  | for (SettleDto dto : list) { | 
|---|
|  |  |  | switch (dto.getStep()) { | 
|---|
|  |  |  | case 1: | 
|---|
|  |  |  | dto.setCurr(Boolean.FALSE); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 2: | 
|---|
|  |  |  | dto.setCurr(Boolean.TRUE); | 
|---|
|  |  |  | dto.setMsg("部门经理" + manager.getNickname() + "审批通过"); | 
|---|
|  |  |  | dto.setTime(DateUtils.convert(now)); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | priQuoteBudget.setSettleMsg(JSON.toJSONString(list)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 修改规划单状态 | 
|---|
|  |  |  | priQuoteBudget.setSettle(2);  // 申请通过 | 
|---|
|  |  |  | priQuoteBudget.setUpdateTime(now); | 
|---|
|  |  |  | if (!priQuoteBudgetService.updateById(priQuoteBudget)) { | 
|---|
|  |  |  | throw new CoolException("审核失败,请联系管理员"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | return R.error("抱歉,您没有审核的权限!!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 2: | 
|---|
|  |  |  | // 业务员 | 
|---|
|  |  |  | User salesman0 = userService.selectById(priQuoteBudget.getUserId()); | 
|---|
|  |  |  | if (!getUserId().equals(salesman0.getId())) { | 
|---|
|  |  |  | return R.error("抱歉,您无需确认!!!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 修改 settle 步骤数据 | 
|---|
|  |  |  | List<SettleDto> list2 = JSON.parseArray(priQuoteBudget.getSettleMsg(), SettleDto.class); | 
|---|
|  |  |  | for (SettleDto dto : list2) { | 
|---|
|  |  |  | switch (dto.getStep()) { | 
|---|
|  |  |  | case 2: | 
|---|
|  |  |  | dto.setCurr(Boolean.TRUE); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | case 3: | 
|---|
|  |  |  | dto.setCurr(Boolean.TRUE); | 
|---|
|  |  |  | dto.setMsg("业务员" + salesman0.getNickname() + "以确认"); | 
|---|
|  |  |  | dto.setTime(DateUtils.convert(new Date())); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | priQuoteBudget.setSettleMsg(JSON.toJSONString(list2)); | 
|---|
|  |  |  | // 修改规划单状态 | 
|---|
|  |  |  | priQuoteBudget.setSettle(3);  // 审批通过 | 
|---|
|  |  |  | priQuoteBudget.setUpdateTime(new Date()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!priQuoteBudgetService.updateById(priQuoteBudget)) { | 
|---|
|  |  |  | throw new CoolException("确认失败,请联系管理员"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | return R.error(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok("审批成功"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|