StoreController.java 9.79 KB
package vion.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import vion.Global;
import vion.enums.ResultEnum;
import vion.model.*;
import vion.service.IAddressSerrvice;
import vion.service.IFileSerrvice;
import vion.service.IStoreSerrvice;
import vion.utils.ResultUtil;
import vion.vo.StatusVO;
import vion.vo.StoreListVO;
import vion.vo.StoreVO;

import java.util.*;

@RestController
@RequestMapping(Global.BASE_URL)
@Slf4j
public class StoreController {

    @Autowired
    private IStoreSerrvice storeSerrvice;
    @Autowired
    private IFileSerrvice fileSerrvice;
    @Autowired
    private IAddressSerrvice addressSerrvice;

    @GetMapping("/stores")
    @ResponseBody
    public Object getStoreList(@RequestParam(name = "accountId",required=false)Integer account_id,
                               @RequestParam(name = "storeId",required=false)Integer store_id,
                               @RequestParam(name = "storenum",required=false)String storenum,
                               @RequestParam(name = "projectState",required=false)Integer project_state,
                               @RequestParam(name = "salesperson",required=false)String salesperson,
                               @RequestParam(name = "implementType",required=false)Integer implement_type,
                               @RequestParam(name = "startdate",required=false)
                                   @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date startdate,
                               @RequestParam(name = "enddate",required=false)
                                   @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date enddate,
                               @RequestParam(name = "pageNum")Integer pageNum,
                               @RequestParam(name = "pageSize")Integer pageSize){

        //pageNum当前页,pageSize每页条数
        Page page= PageHelper.startPage(pageNum,pageSize);

        QueryWrapper<Store> wrapper=new QueryWrapper<>();
        if(store_id!=null){
            wrapper.eq("id",store_id);
        }
        if(account_id!=null)
            wrapper.eq("account_id",account_id);
        if(storenum!=null)
            wrapper.eq("storenum",storenum);
        if(project_state!=null)
            wrapper.eq("project_state",project_state);
        if(salesperson!=null)
            wrapper.eq("salesperson",salesperson);
        if(implement_type!=null)
            wrapper.eq("implement_type",implement_type);
        if(startdate!=null)
            wrapper.between("orderdate",startdate,enddate);


        List<Store> storeList=storeSerrvice.list(wrapper);

        storeList.sort(Comparator.comparing(Store::getId));
        PageInfo pageInfo=new PageInfo<>(page);
        pageInfo.setList(storeList);

        if (pageInfo != null) {
            return ResultUtil.success(pageInfo);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @GetMapping("/store")
    @ResponseBody
    public Object getStoreByID(@RequestParam(name = "id")Integer id){

        StoreVO storeVo=new StoreVO();

        Store store=storeSerrvice.getById(id);
        storeVo.setId(store.getId());
        storeVo.setNumber(store.getNumber());
        storeVo.setName(store.getName());
        storeVo.setStorenum(store.getStorenum());
        storeVo.setAmount(store.getAmount());
        storeVo.setOrderdate(store.getOrderdate());
        storeVo.setSalesperson(store.getSalesperson());
        storeVo.setWarrantyPeriod(store.getWarrantyPeriod());
        storeVo.setCustomerName(store.getCustomerName());
        storeVo.setProjectStage(store.getProjectStage());
        storeVo.setProjectState(store.getProjectState());
        storeVo.setContacts(store.getContacts());
        storeVo.setRemark(store.getRemark());
        storeVo.setAccountId(store.getAccountId());
        storeVo.setContractCode(store.getContractCode());
        storeVo.setImplementType(store.getImplementType());
        //获取地址信息
        Address address=addressSerrvice.getOne(Wrappers.<Address>lambdaQuery()
                .eq(Address::getStoreId,id));
        if(address!=null){
            storeVo.setConsigneeName(address.getConsigneeName());
            storeVo.setConsigneePhone(address.getConsigneePhone());
            storeVo.setConsigneeAddress(address.getConsigneeAddress());
            storeVo.setContractName(address.getContractName());
            storeVo.setContractPhone(address.getContractPhone());
            storeVo.setContractAddress(address.getContractAddress());
            storeVo.setInvoiceName(address.getInvoiceName());
            storeVo.setInvoicePhone(address.getInvoicePhone());
            storeVo.setInvoiceAddress(address.getInvoiceAddress());
            storeVo.setInvoiceInfo(address.getInvoiceInfo());
        }

        if (storeVo != null) {
            return ResultUtil.success(storeVo);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }
    @GetMapping("/storeList")
    @ResponseBody
    public Object getStoreList(@RequestParam(name = "accountId",required=false)Integer account_id){

        QueryWrapper<Store> wrapper=new QueryWrapper<>();

        if(account_id!=null)
            wrapper.eq("account_id",account_id);

        List<Store> storeList=storeSerrvice.list(wrapper);
        List<StoreListVO> storeListVOS=new ArrayList<>();
        for (Store store : storeList){
            StoreListVO storeListVO=new StoreListVO();
            storeListVO.setId(store.getId());
            storeListVO.setName(store.getName());

            storeListVOS.add(storeListVO);
        }

        storeListVOS.sort(Comparator.comparing(StoreListVO::getId));
        if (storeListVOS != null) {
            return ResultUtil.success(storeListVOS);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @PostMapping("/stores")
    @ResponseBody
    public Object saveOrUpdate(@RequestBody StoreVO storeVo,@RequestAttribute String userid) {

        User user=Global.USERNAME_MAP.get(userid);

        Store store=new Store();
        store.setId(storeVo.getId());
        store.setNumber(storeVo.getNumber());
        store.setName(storeVo.getName());
        store.setStorenum(storeVo.getStorenum());
        store.setAmount(storeVo.getAmount());
        store.setOrderdate(storeVo.getOrderdate());
        store.setSalesperson(storeVo.getSalesperson());
        store.setWarrantyPeriod(storeVo.getWarrantyPeriod());
        store.setCustomerName(storeVo.getCustomerName());
        store.setProjectStage(storeVo.getProjectStage());
        store.setProjectState(storeVo.getProjectState());
        store.setContacts(storeVo.getContacts());
        store.setContractCode(storeVo.getContractCode());
        store.setImplementType(storeVo.getImplementType());
        store.setRemark(storeVo.getRemark());
        store.setAccountId(storeVo.getAccountId());
        if(storeVo.getId()!=null) {
            store.setModifyUser(user.getId());
        }else{
            store.setCreateUser(user.getId());
        }


        try {
            Store store1= storeSerrvice.saveAndReturn(store);

            Address address=addressSerrvice.getOne(Wrappers.<Address>lambdaQuery()
                    .eq(Address::getStoreId,store1.getId()));
            if(address==null){
                address=new Address();
                address.setStoreId(store1.getId());
            }
            address.setConsigneeName(storeVo.getConsigneeName());
            address.setConsigneePhone(storeVo.getConsigneePhone());
            address.setConsigneeAddress(storeVo.getConsigneeAddress());
            address.setContractName(storeVo.getContractName());
            address.setContractPhone(storeVo.getContractPhone());
            address.setContractAddress(storeVo.getContractAddress());
            address.setInvoiceName(storeVo.getInvoiceName());
            address.setInvoicePhone(storeVo.getInvoicePhone());
            address.setInvoiceAddress(storeVo.getInvoiceAddress());
            address.setInvoiceInfo(storeVo.getInvoiceInfo());

            addressSerrvice.saveOrUpdate(address);

            return ResultUtil.success();
        }catch (Exception e) {
           // return ResultUtil.error(ResultEnum.SELECT_ERROR);
            return e.getMessage().toString();
        }
    }

    @PostMapping("/updateStoreStatus")
    @ResponseBody
    public Object updateStoreStage(@RequestBody StatusVO statusVo) {

        try {
            //更新项目阶段
            //storeDao.updateStoreStage(statusVo.getSource_id(),statusVo.getProject_stage());
            UpdateWrapper updateWrapper=new UpdateWrapper();
            updateWrapper.eq("id",statusVo.getSourceId());
            updateWrapper.set("project_stage",statusVo.getProjectStage());
            storeSerrvice.update(updateWrapper);

            //添加文件附件
            File file=new File();
            file.setStoreId(statusVo.getStoreId());
            file.setSourceId(statusVo.getSourceId());
            file.setSourceType(statusVo.getSourceType());//1项目、2工单预处理,3工单操作,4巡检
            file.setName(statusVo.getName());
            file.setUrl(statusVo.getUrl());
            file.setType(statusVo.getType());
            fileSerrvice.save(file);

            return ResultUtil.success();
        }catch (Exception e) {
           // return ResultUtil.error(ResultEnum.SELECT_ERROR);
            return e.getMessage().toString();
        }
    }
}