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.annotations.ManagerAuth; 
 | 
import com.core.common.BaseRes; 
 | 
import com.core.common.Cools; 
 | 
import com.core.common.DateUtils; 
 | 
import com.core.common.R; 
 | 
import com.core.domain.KeyValueVo; 
 | 
import com.zy.crm.common.web.BaseController; 
 | 
import com.zy.crm.manager.entity.Company; 
 | 
import com.zy.crm.manager.service.CompanyService; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.web.bind.annotation.*; 
 | 
  
 | 
import java.util.*; 
 | 
  
 | 
@RestController 
 | 
public class CompanyController extends BaseController { 
 | 
  
 | 
    @Autowired 
 | 
    private CompanyService companyService; 
 | 
  
 | 
    @RequestMapping(value = "/company/{id}/auth") 
 | 
    @ManagerAuth 
 | 
    public R get(@PathVariable("id") String id) { 
 | 
        return R.ok(companyService.selectById(String.valueOf(id))); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/company/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<Company> wrapper = new EntityWrapper<>(); 
 | 
        excludeTrash(param); 
 | 
        convert(param, wrapper); 
 | 
        allLike(Company.class, param.keySet(), wrapper, condition); 
 | 
        if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));} 
 | 
        return R.ok(companyService.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 = "/company/add/auth") 
 | 
    @ManagerAuth 
 | 
    public R add(Company company) { 
 | 
        company.setHostId(getHostId()); 
 | 
        company.setCreateBy(getUserId()); 
 | 
        company.setCreateTime(new Date()); 
 | 
        company.setUpdateBy(getUserId()); 
 | 
        company.setUpdateTime(new Date()); 
 | 
        company.setStatus(1); 
 | 
        companyService.insert(company); 
 | 
        return R.ok(); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/company/update/auth") 
 | 
    @ManagerAuth 
 | 
    public R update(Company company){ 
 | 
        if (Cools.isEmpty(company) || null==company.getId()){ 
 | 
            return R.error(); 
 | 
        } 
 | 
        company.setUpdateBy(getUserId()); 
 | 
        company.setUpdateTime(new Date()); 
 | 
        companyService.updateById(company); 
 | 
        return R.ok(); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/company/delete/auth") 
 | 
    @ManagerAuth 
 | 
    public R delete(@RequestParam(value="ids[]") Long[] ids){ 
 | 
         for (Long id : ids){ 
 | 
            companyService.deleteById(id); 
 | 
        } 
 | 
        return R.ok(); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/company/export/auth") 
 | 
    @ManagerAuth 
 | 
    public R export(@RequestBody JSONObject param){ 
 | 
        EntityWrapper<Company> wrapper = new EntityWrapper<>(); 
 | 
        List<String> fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class); 
 | 
        Map<String, Object> map = excludeTrash(param.getJSONObject("company")); 
 | 
        convert(map, wrapper); 
 | 
        List<Company> list = companyService.selectList(wrapper); 
 | 
        return R.ok(exportSupport(list, fields)); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/companyQuery/auth") 
 | 
    @ManagerAuth 
 | 
    public R query(String condition) { 
 | 
        EntityWrapper<Company> wrapper = new EntityWrapper<>(); 
 | 
        wrapper.like("name", condition); 
 | 
        Page<Company> page = companyService.selectPage(new Page<>(0, 10), wrapper); 
 | 
        List<Map<String, Object>> result = new ArrayList<>(); 
 | 
        for (Company company : page.getRecords()){ 
 | 
            Map<String, Object> map = new HashMap<>(); 
 | 
            map.put("id", company.getId()); 
 | 
            map.put("value", company.getId()); 
 | 
            result.add(map); 
 | 
        } 
 | 
        return R.ok(result); 
 | 
    } 
 | 
  
 | 
    @RequestMapping(value = "/company/check/column/auth") 
 | 
    @ManagerAuth 
 | 
    public R query(@RequestBody JSONObject param) { 
 | 
        Wrapper<Company> wrapper = new EntityWrapper<Company>().eq(humpToLine(String.valueOf(param.get("key"))), param.get("val")); 
 | 
        if (null != companyService.selectOne(wrapper)){ 
 | 
            return R.parse(BaseRes.REPEAT).add(getComment(Company.class, String.valueOf(param.get("key")))); 
 | 
        } 
 | 
        return R.ok(); 
 | 
    } 
 | 
  
 | 
    // xm-select 
 | 
    @RequestMapping("/company/all/get/kv") 
 | 
    @ManagerAuth 
 | 
    public R getDataKV(@RequestParam(required = false) String condition) { 
 | 
        Wrapper<Company> wrapper = new EntityWrapper<Company>() 
 | 
                .andNew().like("name", condition).or().like("uuid", condition) 
 | 
                .orderBy("create_time", false); 
 | 
        List<Company> items = companyService.selectPage(new Page<>(1, 30), wrapper).getRecords(); 
 | 
        List<KeyValueVo> valueVos = new ArrayList<>(); 
 | 
        for (Company item : items) { 
 | 
            valueVos.add(new KeyValueVo(item.getName(), item.getId())); 
 | 
        } 
 | 
        return R.ok().add(valueVos); 
 | 
    } 
 | 
  
 | 
} 
 |