package com.zy.crm.manager.controller; 
 | 
  
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.baomidou.mybatisplus.mapper.EntityWrapper; 
 | 
import com.baomidou.mybatisplus.mapper.Wrapper; 
 | 
import com.baomidou.mybatisplus.plugins.Page; 
 | 
import com.core.common.DateUtils; 
 | 
import com.zy.crm.manager.entity.Cstmr; 
 | 
import com.zy.crm.manager.entity.Item; 
 | 
import com.zy.crm.manager.service.CstmrService; 
 | 
import com.zy.crm.manager.service.ItemService; 
 | 
import com.core.annotations.ManagerAuth; 
 | 
import com.core.common.BaseRes; 
 | 
import com.core.common.Cools; 
 | 
import com.core.common.R; 
 | 
import com.core.domain.KeyValueVo; 
 | 
import com.zy.crm.common.web.BaseController; 
 | 
import com.zy.crm.system.entity.User; 
 | 
import com.zy.crm.system.service.UserService; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.web.bind.annotation.*; 
 | 
  
 | 
import java.text.SimpleDateFormat; 
 | 
import java.util.*; 
 | 
  
 | 
@RestController 
 | 
public class ItemController extends BaseController { 
 | 
  
 | 
    @Autowired 
 | 
    private ItemService itemService; 
 | 
  
 | 
    @Autowired 
 | 
    private CstmrService cstmrService; 
 | 
  
 | 
    @RequestMapping(value = "/item/{id}/auth") 
 | 
    @ManagerAuth 
 | 
    public R get(@PathVariable("id") Long id) { 
 | 
        return R.ok(itemService.selectById(id)); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/item/list/auth") 
 | 
    @ManagerAuth 
 | 
    public R list(@RequestParam(defaultValue = "1")Integer curr, 
 | 
                  @RequestParam(defaultValue = "10")Integer limit, 
 | 
                  @RequestParam(required = false)String orderByField, 
 | 
                  @RequestParam(required = false)String orderByType, 
 | 
                  @RequestParam(required = false)String condition, 
 | 
                  @RequestParam Map<String, Object> param){ 
 | 
        EntityWrapper<Item> wrapper = new EntityWrapper<>(); 
 | 
        wrapper.in("member", getUserRoleBelongsToUserId("allopen")); 
 | 
        excludeTrash(param); 
 | 
        convert(param, wrapper); 
 | 
        allLike(Item.class, param.keySet(), wrapper, condition); 
 | 
        if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));} 
 | 
        return R.ok(itemService.selectPage(new Page<>(curr, limit), wrapper)); 
 | 
    } 
 | 
  
 | 
    private <T> void convert(Map<String, Object> map, EntityWrapper<T> wrapper){ 
 | 
        for (Map.Entry<String, Object> entry : map.entrySet()){ 
 | 
            String val = String.valueOf(entry.getValue()); 
 | 
            if (val.contains(RANGE_TIME_LINK)){ 
 | 
                String[] dates = val.split(RANGE_TIME_LINK); 
 | 
                wrapper.ge(entry.getKey(), DateUtils.convert(dates[0])); 
 | 
                wrapper.le(entry.getKey(), DateUtils.convert(dates[1])); 
 | 
            } else { 
 | 
                wrapper.like(entry.getKey(), val); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/item/add/auth") 
 | 
    @ManagerAuth 
 | 
    public R add(Item item) { 
 | 
        Random random = new Random(); 
 | 
        SimpleDateFormat orderNumFormat = new SimpleDateFormat("yyyyMMdd"); 
 | 
        SimpleDateFormat inOrderNumFormat = new SimpleDateFormat("yyyyMMddHHmmss"); 
 | 
        String orderNum = orderNumFormat.format(new Date()).toString() + random.nextInt(9999); 
 | 
        String inOrderNum = inOrderNumFormat.format(new Date()).toString() + random.nextInt(9999); 
 | 
  
 | 
        item.setOrderNum(orderNum); 
 | 
        item.setHostId(Long.parseLong("1")); 
 | 
        item.setInOrderNum(inOrderNum); 
 | 
        item.setStartTime(new Date()); 
 | 
        item.setStep(1); 
 | 
  
 | 
        //获取客户信息 
 | 
        Cstmr cstmr = cstmrService.selectByName(item.getHostId(), item.getCstmr()); 
 | 
        if (Cools.isEmpty(cstmr)) { 
 | 
            return R.error("客户不存在"); 
 | 
        } 
 | 
  
 | 
        item.setCstmrUuid(cstmr.getUuid()); 
 | 
        itemService.insert(item); 
 | 
        return R.ok(); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/item/update/auth") 
 | 
    @ManagerAuth 
 | 
    public R update(Item item){ 
 | 
        //获取客户信息 
 | 
        Cstmr cstmr = cstmrService.selectByName(item.getHostId(), item.getCstmr()); 
 | 
        if (cstmr == null) { 
 | 
            return R.error("客户不存在"); 
 | 
        } 
 | 
        if (item.getStatus() == 3) { 
 | 
            item.setEndTime(new Date()); 
 | 
        } 
 | 
  
 | 
        item.setUpdateTime(new Date()); 
 | 
        itemService.updateById(item); 
 | 
        return R.ok(); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/item/delete/auth") 
 | 
    @ManagerAuth 
 | 
    public R delete(Long[] ids){ 
 | 
        if (Cools.isEmpty(ids)){ 
 | 
            return R.error(); 
 | 
        } 
 | 
        itemService.deleteBatchIds(Arrays.asList(ids)); 
 | 
        return R.ok(); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/item/export/auth") 
 | 
    @ManagerAuth 
 | 
    public R export(@RequestBody JSONObject param){ 
 | 
        EntityWrapper<Item> wrapper = new EntityWrapper<>(); 
 | 
        List<String> fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class); 
 | 
        Map<String, Object> map = excludeTrash(param.getJSONObject("pri")); 
 | 
        convert(map, wrapper); 
 | 
        List<Item> list = itemService.selectList(wrapper); 
 | 
        return R.ok(exportSupport(list, fields)); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/itemQuery/auth") 
 | 
    @ManagerAuth 
 | 
    public R query(String condition) { 
 | 
        EntityWrapper<Item> wrapper = new EntityWrapper<>(); 
 | 
        wrapper.like("id", condition); 
 | 
        wrapper.in("member", getUserRoleBelongsToUserId("allopen")); 
 | 
        Page<Item> page = itemService.selectPage(new Page<>(0, 10), wrapper); 
 | 
        List<Map<String, Object>> result = new ArrayList<>(); 
 | 
        for (Item item : page.getRecords()){ 
 | 
            Map<String, Object> map = new HashMap<>(); 
 | 
            map.put("id", item.getId()); 
 | 
            map.put("value", item.getName()); 
 | 
            result.add(map); 
 | 
        } 
 | 
        return R.ok(result); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/item/check/column/auth") 
 | 
    @ManagerAuth 
 | 
    public R query(@RequestBody JSONObject param) { 
 | 
        Wrapper<Item> wrapper = new EntityWrapper<Item>().eq(humpToLine(String.valueOf(param.get("key"))), param.get("val")); 
 | 
        if (null != itemService.selectOne(wrapper)){ 
 | 
            return R.parse(BaseRes.REPEAT).add(getComment(Item.class, String.valueOf(param.get("key")))); 
 | 
        } 
 | 
        return R.ok(); 
 | 
    } 
 | 
  
 | 
    @RequestMapping("/item/all/get/kv") 
 | 
    @ManagerAuth 
 | 
    public R getDataKV(@RequestParam(required = false) String condition) { 
 | 
        List<KeyValueVo> vos = new ArrayList<>(); 
 | 
        Wrapper<Item> wrapper = new EntityWrapper<Item>().andNew().like("id", condition).orderBy("create_time", false); 
 | 
        itemService.selectPage(new Page<>(1, 30), wrapper).getRecords().forEach(item -> vos.add(new KeyValueVo(String.valueOf(item.getId()), item.getId()))); 
 | 
        return R.ok().add(vos); 
 | 
    } 
 | 
  
 | 
} 
 |