package com.yijia.station.controller;

import com.yijia.common.annotation.Log;
import com.yijia.common.core.controller.BaseController;
import com.yijia.common.core.domain.AjaxResult;
import com.yijia.common.core.domain.entity.SysDept;
import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.enums.BusinessType;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.station.domain.StationPic;
import com.yijia.station.domain.SysDeptDemo;
import com.yijia.system.service.ISysDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 油站支付设置Controller
 *
 * @author yijia
 * @date 2020-12-10
 */
@RestController
@RequestMapping("/station/pay")
public class StationPayController extends BaseController
{
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 获取油站设置详细信息
     */
    @GetMapping(value = "/{payId}")
    public AjaxResult getInfo(@PathVariable("payId") Long payId)
    {
        return AjaxResult.success(sysDeptService.selectDeptById(payId));
    }

    /**
     * 查询油站支付设置信息
     * @param
     * @return
     */
    @GetMapping(value = "/queryOne")
    public AjaxResult queryOne(SysDept sysDept)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDeptDemo deptDemo =new SysDeptDemo();
        SysDept dept= sysDeptService.selectDeptById(currentUser.getUser().getDeptId());
        if(dept!=null){
            BeanUtils.copyProperties(dept, deptDemo);
            List<StationPic> list =new ArrayList<>();
            if(dept.getImage1()!=null && !dept.getImage1().equals("")){
                StationPic pic=new StationPic();
                pic.setName("1");
                pic.setUrl(dept.getImage1());
                list.add(pic);
            }
            if(dept.getImage2()!=null && !dept.getImage2().equals("")){
                StationPic pic=new StationPic();
                pic.setName("2");
                pic.setUrl(dept.getImage2());
                list.add(pic);
            }
            if(dept.getImage3()!=null && !dept.getImage3().equals("")){
                StationPic pic=new StationPic();
                pic.setName("3");
                pic.setUrl(dept.getImage3());
                list.add(pic);
            }
            if(dept.getImage4()!=null && !dept.getImage4().equals("")){
                StationPic pic=new StationPic();
                pic.setName("4");
                pic.setUrl(dept.getImage4());
                list.add(pic);
            }
            if(dept.getImage5()!=null && !dept.getImage5().equals("")){
                StationPic pic=new StationPic();
                pic.setName("5");
                pic.setUrl(dept.getImage5());
                list.add(pic);
            }
            deptDemo.setImgFileList(list);
        }
        return AjaxResult.success(deptDemo);
    }
    /***
     * 根据油站id 查询出油站信息
     * @param
     * @return
     */
    @GetMapping(value = "/{stationId}")
    public AjaxResult getPayInfo(@PathVariable("stationId") Long stationId)
    {
        return AjaxResult.success(sysDeptService.selectDeptById(stationId));
    }
    /**
     * 修改油站支付设置
     */
    @Log(title = "油站设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDeptDemo sysDeptDemo){
        sysDeptDemo.setImage1("");
        sysDeptDemo.setImage2("");
        sysDeptDemo.setImage3("");
        sysDeptDemo.setImage4("");
        sysDeptDemo.setImage5("");
        if(sysDeptDemo.getImgFileList()!=null && sysDeptDemo.getImgFileList().size()>0){
            for(int i=0;i<sysDeptDemo.getImgFileList().size();i++){
                if(i==0){
                    sysDeptDemo.setImage1(sysDeptDemo.getImgFileList().get(i).getUrl());
                }else if(i==1){
                    sysDeptDemo.setImage2(sysDeptDemo.getImgFileList().get(i).getUrl());
                }else if(i==2){
                    sysDeptDemo.setImage3(sysDeptDemo.getImgFileList().get(i).getUrl());
                }else if(i==3){
                    sysDeptDemo.setImage4(sysDeptDemo.getImgFileList().get(i).getUrl());
                }else if(i==4){
                    sysDeptDemo.setImage5(sysDeptDemo.getImgFileList().get(i).getUrl());
                }
            }
        }
        SysDept dept = new SysDept();
        BeanUtils.copyProperties(sysDeptDemo, dept);
        return toAjax(sysDeptService.updateDept(dept));
    }
}
